This example will demonstrate creating a Multi-Part mime message. Multi-Part
messages are messages that contain alternate body parts. Alternate body parts
are used for displaying different content to different mail clients. Because it
is impossible to sniff or determine an end users' mail client application, it is
left up to the email developer to cover all formats. You simply create
different, alternate body parts. Then, it is up to the mail client to display
the richest body part it can render.
The following example creates the most
common Multi-Part mime email, a Plain Text and a Html Text email (2 alternate
body parts).
[ C# ]
static void MultiPartMime()
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");
//set the content
mail.Subject = "This is an email";
//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
//then we create the Html part
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);
}
[ VB.NET ]
Sub MultiPartMime()
'create the mail message
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")
'set the content
mail.Subject = "This is an email"
'first we create the Plain Text part
Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", Nothing, "text/plain")
'then we create the Html part
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", Nothing, "text/html")
mail.AlternateViews.Add(plainView)
mail.AlternateViews.Add(htmlView)
'send the message
Dim smtp As New SmtpClient("127.0.0.1") 'specify the mail server address
smtp.Send(mail)
End Sub 'MultiPartMime