55

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 below is not formatted as such.

<p>Welcome to SiteName. To activate your account, visit this URL: 
    <a href="http://SiteName.com/a?key=1234">http://SiteName.com/a?key=1234</a>.
</p>

How do I enable HTML in the e-mail messages I send?

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 1
    ropstah: TBH i was confused and wrote in one of your comments that there is no IsBodyHtml in SmtpClient. Josiah is who showed me to do it with MailMessage. –  Aug 26 '09 at 04:02

6 Answers6

105

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;

Bridge
  • 29,818
  • 9
  • 60
  • 82
Josiah Peters
  • 1,357
  • 1
  • 11
  • 17
  • 10
    Worth noting that both `MailMessage` and `SmtpClient` implement `IDisposable`, and need to be disposed accordingly. – djs Feb 23 '17 at 19:54
  • I use C# windows form to send email in this way. How to embed an image to the email? `string body = "Hi there...

    Test Title

    test body

    Image";' I just added my image to the bin/debug folder. but it is not working.
    – JayNaz Dec 08 '18 at 04:34
22

I believe it was something like:

mailObject.IsBodyHtml = true;
Ropstah
  • 17,538
  • 24
  • 120
  • 194
20

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
faester
  • 14,886
  • 5
  • 45
  • 56
  • Is the `AlternateView` for `htmlView` really necessary? Because you already set the body to be `htmlText`. Isn't it redundant to set it again as alternate view? – Rosdi Kasim Jan 22 '16 at 10:00
  • 2
    Not only it's necessary, but also important that it's added after the plain alternate view. see http://stackoverflow.com/questions/5188605/gmail-displays-plain-text-email-instead-html. But specifying Body and IsBodyHtml is not necessary if you have added html alternate view. – FunkyOne Sep 22 '16 at 12:44
8

Apply the correct encoding of the Mailbody.

mail.IsBodyHtml = true;
Bdiem
  • 566
  • 2
  • 8
0

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);
nassimlouchani
  • 423
  • 4
  • 8
-1

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());                
            }
Naveen
  • 1,441
  • 2
  • 16
  • 41