MailMessage.AlternateViews MailMessage.AlternateViews MailMessage.AlternateViews MailMessage.AlternateViews Property


取得附件集合,用於儲存訊息主體的替代格式。Gets the attachment collection used to store alternate forms of the message body.

 property System::Net::Mail::AlternateViewCollection ^ AlternateViews { System::Net::Mail::AlternateViewCollection ^ get(); };
public System.Net.Mail.AlternateViewCollection AlternateViews { get; }
member this.AlternateViews : System.Net.Mail.AlternateViewCollection
Public ReadOnly Property AlternateViews As AlternateViewCollection



下列程式碼範例示範如何建立及傳送含有純文字和 HTML 替代視圖的電子郵件訊息。The following code example demonstrates creating and sending an email message with a plain text and an HTML alternate view.

static void CreateMessageWithMultipleViews( String^ server, String^ recipients )
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"",recipients,L"This email message has multiple views.",L"This is some plain text." );
   // Construct the alternate body as HTML.
   String^ body = L"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
   body = String::Concat( body, L"<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">" );
   body = String::Concat( body, L"</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text" );
   body = String::Concat( body, L"</FONT></DIV></BODY></HTML>" );
   // Add the alternate body to the message.
   AlternateView^ alternate = AlternateView::CreateAlternateViewFromString(body);

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


public static void CreateMessageWithMultipleViews(string server, string recipients)
	// Create a message and set up the recipients.
	MailMessage message = new MailMessage(
		"This email 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);


AlternateViews使用屬性, 即可指定不同格式的電子郵件訊息複本。Use the AlternateViews property to specify copies of an email message in different formats. 例如, 如果您以 HTML 傳送訊息, 可能也會想要提供純文字版本, 以防某些收件者使用無法顯示 HTML 內容的電子郵件讀取器。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 email readers that cannot display HTML content.

若要將替代視圖加入至MailMessage物件, 請Attachment建立 view 的, 然後將它加入AlternateViews至所傳回的集合。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. 使用屬性來指定文字版本, 並AlternateViews使用集合來指定其他 MIME 類型的視圖。 BodyUse the Body property to specify the text version and use the AlternateViews collection to specify views with other MIME types. MediaTypeNames使用類別成員指定替代視圖的 MIME 類型。Use the MediaTypeNames class members to specify the MIME type for the alternate view.