1 out of 1 rated this helpful - Rate this topic

MailMessage.AlternateViews Property

Gets the attachment collection used to store alternate forms of the message body.

Namespace:  System.Net.Mail
Assembly:  System (in System.dll)
public AlternateViewCollection AlternateViews { get; }

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.

To add an alternate view to a MailMessage object, create an Attachment for the view, and then add it to the collection returned by AlternateViews. Use the Body property to specify the text version and use the AlternateViews collection to specify views with other MIME types. Use the MediaTypeNames class members to specify the MIME type for the alternate view.

The following code example demonstrates creating and sending an e-mail message with a plain text and an HTML alternate view.


		public static void CreateMessageWithMultipleViews(string server, string recipients)
		{
			// Create a message and set up the recipients.
			MailMessage message = new MailMessage(
				"jane@contoso.com",
				recipients,
				"This e-mail message has multiple views.",
				"This is some plain text.");

			// Construct the alternate body as HTML.
			string body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
			body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
			body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text";
			body += "</FONT></DIV></BODY></HTML>";

			ContentType mimeType = new System.Net.Mime.ContentType("text/html");
			// Add the alternate body to the message.
			
			AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
			message.AlternateViews.Add(alternate);

			// Send the message.
			SmtpClient client = new SmtpClient(server);
			client.Credentials = CredentialCache.DefaultNetworkCredentials;

      try {
			  client.Send(message);
			}
			catch (Exception ex) {
			  Console.WriteLine("Exception caught in CreateMessageWithMultipleViews(): {0}", 
                    ex.ToString() );			  
      }
			// Display the values in the ContentType for the attachment.
			ContentType c = alternate.ContentType;
			Console.WriteLine("Content type");
			Console.WriteLine(c.ToString());
			Console.WriteLine("Boundary {0}", c.Boundary);
			Console.WriteLine("CharSet {0}", c.CharSet);
			Console.WriteLine("MediaType {0}", c.MediaType);
			Console.WriteLine("Name {0}", c.Name);
			Console.WriteLine("Parameters: {0}", c.Parameters.Count);
			foreach (DictionaryEntry d in c.Parameters)
			{
				Console.WriteLine("{0} = {1}", d.Key, d.Value);
			}
			Console.WriteLine();
			alternate.Dispose();
		}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
When creating from stream, afterwards set .Position=0
When the alternate view is created from a MemoryStream, afterwards you must set MemoryStream.Position = 0 or when you use SmtpClient.Send(MailMessage), you will end up with a blank message, as it sends from the current AlternateView.ContentStream position.

I don't know why MailMessage doesn't automatically reset the position and send the entire content each time, but it doesn't.

I assume the same is true when creating Attachments from a stream.

Word "Attachment" should be "AltenateView"
Text is incorrect (and has been since at least .NET 2.0)

"To add an alternate view to a MailMessage object, create an Attachment for the view,"

instead of "Attachment", it should read "AlternateView"

Use System.Net.Mime.MediaTypeNames.Text instead of writing your own mime text
intead of using

       ContentType mimeType = new System.Net.Mime.ContentType("text/html");
        // Add the alternate body to the message.

        AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
        message.AlternateViews.Add(alternate);

You could use the System.Net.Mime.MediaTypeNames.Text static type

        AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, null, System.Net.Mime.MediaTypeNames.Text.Html);
        message.AlternateViews.Add(alternate);

OR

       ContentType mimeType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html);

        AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
        message.AlternateViews.Add(alternate);
Order of alternate views is important

The order in which multiple alternate views are added is important for some mail agents.

According to the RFC 2046, the last listed format (in the raw message) that a mail reader (agent) can understand should be displayed.

To achieve that with System.Net.Mail.MailMessage.AlternateViews.Add add alternates in order from least desirable to most desirable.  i.e text/plain before text/html.

  1. mail.AlternateViews.Add(leastdesirable)
  2. mail.AlternateViews.Add(mostdesirable)

Some samples on the web don't do this, and can mislead you.  With some mail agents you won't see the problem.  For example they may display html first irrespective of the order  in which it appears.