Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
AlternateViews Property

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(
				"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);

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

      try {
			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("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);
     static void CreateMessageWithMultipleViews(String* server, String* recipients)
        // Create a message and set up the recipients.
        MailMessage* message = new MailMessage(
            S"This e-mail message has multiple views.", 
            S"This is some plain text.");

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

        // Add the alternate body to the message.
        Attachment* alternate = new Attachment();
        alternate->SetContent(body, MediaTypeNames::Text::Html);

         // Send the message.
        SmtpClient* client = new SmtpClient(server);
        client->Credentials = CredentialCache::DefaultNetworkCredentials;
         // Display the values in the ContentType for the attachment.
        ContentType* c = alternate->ContentType;
        Console::WriteLine(S"Content type");
        Console::WriteLine(S"Boundary {0}",c->Boundary);
        Console::WriteLine(S"CharSet {0}",c->CharSet);
        Console::WriteLine(S"MediaType {0}",c->MediaType);
        Console::WriteLine(S"Name {0}",c->Name);
        Console::WriteLine(S"Parameters: {0}", __box(c->Parameters->Count));
        IEnumerator* myEnum = c->Parameters->GetEnumerator();
        while (myEnum->MoveNext())
            DictionaryEntry* d = __try_cast<DictionaryEntry*>(myEnum->Current);
            Console::WriteLine(S"{0} = {1}", d->Key, d->Value);

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

© 2015 Microsoft