MailMessage.AlternateViews 속성

정의

메시지 본문의 대체 형식을 저장하는 데 사용되는 첨부 파일 컬렉션을 가져옵니다.

public:
 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

속성 값

쓰기 가능한 AlternateViewCollection입니다.

예제

다음 코드 예제에서는 일반 텍스트와 HTML 대체 보기를 사용하여 전자 메일 메시지를 만들고 보내는 방법을 보여 줍니다.

static void CreateMessageWithMultipleViews( String^ server, String^ recipients )
{
   
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"jane@contoso.com",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);
   message->AlternateViews->Add(alternate);

   // 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 );
   }

   Console::WriteLine();
   alternate->~AlternateView();
}
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 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);
    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();
}
Public Shared Sub CreateMessageWithMultipleViews(ByVal server As String, ByVal recipients As String)
    ' Create a message and set up the recipients.
    Dim message As MailMessage = New MailMessage(
        "jane@contoso.com",
        recipients,
        "This email message has multiple views.",
        "This is some plain text.")

    ' Construct the alternate body as HTML.
    Dim body As String = "<!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>"

    Dim mimeType As ContentType = New System.Net.Mime.ContentType("text/html")

    ' Add the alternate body to the message.
    Dim alternate As AlternateView = AlternateView.CreateAlternateViewFromString(body, mimeType)
    message.AlternateViews.Add(alternate)

    ' Send the message.
    Dim client As SmtpClient = New SmtpClient(server)
    client.Credentials = CredentialCache.DefaultNetworkCredentials

    Try
        client.Send(message)
    Catch ex As Exception
        Console.WriteLine("Exception caught in CreateMessageWithMultipleViews(): {0}", ex.ToString())
    End Try

    ' Display the values in the ContentType for the attachment.
    Dim c As ContentType = 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)

    For Each d As DictionaryEntry In c.Parameters
        Console.WriteLine("{0} = {1}", d.Key, d.Value)
    Next

    Console.WriteLine()
    alternate.Dispose()
End Sub

설명

사용 된 AlternateViews 다양 한 형식의 전자 메일 메시지의 복사본을 지정 하는 속성입니다. 예를 들어 HTML로 메시지를 보내는 경우 일부 받는 사람이 HTML 콘텐츠를 표시할 수 없는 전자 메일 읽기 프로그램을 사용하는 경우 일반 텍스트 버전을 제공할 수도 있습니다.

개체에 대체 뷰를 MailMessage 추가하려면 뷰에 대한 을 Attachment 만든 다음 에서 반환된 컬렉션에 AlternateViews추가합니다. 사용 된 Body 텍스트 버전을 지정 하 고 다른 MIME 형식으로 AlternateViews 보기를 지정 하는 컬렉션을 사용 하 여 속성입니다. 클래스 멤버를 MediaTypeNames 사용하여 대체 보기에 대한 MIME 형식을 지정합니다.

적용 대상