56

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?

1
  • 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. Commented Aug 26, 2009 at 4:02

6 Answers 6

106

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;

Sign up to request clarification or add additional context in comments.

2 Comments

Worth noting that both MailMessage and SmtpClient implement IDisposable, and need to be disposed accordingly.
I use C# windows form to send email in this way. How to embed an image to the email? `string body = "Hi there... <h1> Test Title </h1> </br> <h3> test body </h3> <img src='image.jpg' alt='Image'>";' I just added my image to the bin/debug folder. but it is not working.
22

I believe it was something like:

mailObject.IsBodyHtml = true;

Comments

21

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

2 Comments

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?
Not only it's necessary, but also important that it's added after the plain alternate view. see stackoverflow.com/questions/5188605/…. But specifying Body and IsBodyHtml is not necessary if you have added html alternate view.
8

Apply the correct encoding of the Mailbody.

mail.IsBodyHtml = true;

Comments

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);

Comments

-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, [email protected]));
            mailMessage.Sender = new MailboxAddress(senderName, [email protected]);
            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("[email protected]", "password");

                    smtpClient.Send(mailMessage);
                    Console.WriteLine("Success");
                }
            }
            catch (SmtpCommandException ex)
            {
                Console.WriteLine(ex.ToString());              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());                
            }

3 Comments

The builder is not used in this context
For BodyBuilder you must have MimeKit.
No, i meant after builder.HtmlBody = "Hello There"; builder variable is not used at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.