Send HTML email via C# with SmtpClient
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;
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 bodymsg.IsBodyHtml = true; // But the basis is the html body