<<Back To Faq 2.5


Complete FAQ Listing


2.5 AlternateView Class Printer Friendly


Click here for MSDN docs.

The AlternateView class is used for providing alternate bodies and creating Multi-Part mime emails. If you want to create an email that will be rendered properly in both Html capable and Plain Text only mail clients, then you will create alternate views of the message. There are a few main properties and methods you will use with the AlternateView class. They are:

Name Description
BaseUri Gets or sets the Base URI to use for resolving relative URIs in the AlternateView
LinkedResources Gets the set of embedded resources referred to by this attachment.
CreateAlternateViewFromString (static method) Overloaded. Creates an AlternateView to view an email message using the specified format..

Below is an example demonstrating the AlternateView class.

 

[ 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



 



The formatted version of this faq can be found at http://www.SystemNetMail.com/faq/2.5.aspx