How to add Alternate views for email in C#.net

Use the AlternateViews property to specify copies of an e-mail message in different formats. For example, if you send a message in HTML, you might also want to provide a plain text version in case some of the recipients use e-mail readers that cannot display HTML content.

Code :-

using System.Net.Mail; // import System.Net.Mail;
//serverDetails – Your smtp server details
//recipient    – To mail ID
public void MultipleViewsMailSend(string serverDetails, string recipient)
{
MailMessage msg = new MailMessage(
“Youremailid@domain.com”,
recipient,
“This is demo mail.”,
“This is just demo mail with plain text“);

// Construct the alternate body as HTML.
string mailBody = “<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>”;
mailBody+= “<HTML><HEAD><META http-equiv=Content-Type content=”text/html; charset=iso-8859-1″>”;
mailBody+= “</HEAD><BODY><DIV><FONT face=Arial color=#0f0 size=2>This is just demo mail with html text“;
mailBody+= “</FONT></DIV></BODY></HTML>”;

ContentType mimeType = new System.Net.Mime.ContentType(“text/html”);

AlternateView altView = AlternateView.CreateAlternateViewFromString(mailBody, mimeType);
message.AlternateViews.Add(altView);

// Send the message.
SmtpClient smtpcln = new SmtpClient(serverDetails);
smtpcln.Credentials = CredentialCache.DefaultNetworkCredentials;

smtpcln.Send(msg);
altView.Dispose();
}

I hope this code will be helpful for you. If any best way then this, please share with me.

Enjoy while coding..!

Thanks,

Naga Harish Movva.

Leave a Reply