Send HTML email via C# with SmtpClient

后端 未结 6 666
野趣味
野趣味 2020-11-28 08:43

How do I send an HTML email? I use the code in this answer to send emails with SmtpClient, but they\'re always plain text, so the link in the example message be

相关标签:
6条回答
  • 2020-11-28 08:53

    IsBodyHtml = true is undoubtedly the most important part.

    But if you want to provide an email with both a text/plain part and a text/html part composed as alternates, it is also possible using the AlternateView class:

    MailMessage msg = new MailMessage();
    AlternateView plainView = AlternateView
         .CreateAlternateViewFromString("Some plaintext", Encoding.UTF8, "text/plain");
    // We have something to show in real old mail clients.
    msg.AlternateViews.Add(plainView); 
    string htmlText = "The <b>fancy</b> part.";
    AlternateView htmlView = 
         AlternateView.CreateAlternateViewFromString(htmlText, Encoding.UTF8, "text/html");
    msg.AlternateViews.Add(htmlView); // And a html attachment to make sure.
    msg.Body = htmlText;  // But the basis is the html body
    msg.IsBodyHtml = true; // But the basis is the html body
    
    0 讨论(0)
  • 2020-11-28 08:55

    i have an idea , you can add a check box to your project for sending email as html as an option for user , and add this code to enable it :

             MailMessage mail = new MailMessage(from, to, subject, message);
    
             if(checkBox1.CheckState == CheckState.Checked )
               {
                   mail.IsBodyHtml = true;
               }
    
             SmtpClient client = new SmtpClient("localhost");
    
             client.Send(mail);
    
    0 讨论(0)
  • 2020-11-28 09:00

    I believe it was something like:

    mailObject.IsBodyHtml = true;
    
    0 讨论(0)
  • 2020-11-28 09:01

    If you are using Mailkit,We can use TextBody,HtmlBody and Both for message body. Just write this code. It will help you

                MimeMessage mailMessage = new MimeMessage();
                mailMessage.From.Add(new MailboxAddress(senderName, sender@address.com));
                mailMessage.Sender = new MailboxAddress(senderName, sender@address.com);
                mailMessage.To.Add(new MailboxAddress(emailid, emailid));
                mailMessage.Subject = subject;
                mailMessage.ReplyTo.Add(new MailboxAddress(replyToAddress));
                mailMessage.Subject = subject;
                var builder = new BodyBuilder();
                builder.HtmlBody = "Hello There";
                mailMessage.Body = builder.ToMessageBody();            
                try
                {
                    using (var smtpClient = new SmtpClient())
                    {
                        smtpClient.Connect("HostName", "Port", MailKit.Security.SecureSocketOptions.None);
                        smtpClient.Authenticate("user@name.com", "password");
    
                        smtpClient.Send(mailMessage);
                        Console.WriteLine("Success");
                    }
                }
                catch (SmtpCommandException ex)
                {
                    Console.WriteLine(ex.ToString());              
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());                
                }
    
    0 讨论(0)
  • 2020-11-28 09:03

    This is what I do:

    MailMessage mail = new MailMessage(from, to, subject, message);
    mail.IsBodyHtml = true;
    SmtpClient client = new SmtpClient("localhost");
    client.Send(mail);
    

    Note that I set the mail message html to true: mail.IsBodyHtml = true;

    0 讨论(0)
  • 2020-11-28 09:05

    Apply the correct encoding of the Mailbody.

    mail.IsBodyHtml = true;
    
    0 讨论(0)
提交回复
热议问题