MailMessage 클래스

정의

SmtpClient 클래스를 사용하여 보낼 수 있는 이메일 메시지를 나타냅니다.

public ref class MailMessage : IDisposable
public class MailMessage : IDisposable
type MailMessage = class
    interface IDisposable
Public Class MailMessage
Implements IDisposable
상속
MailMessage
구현

예제

다음 코드 예제에서는 첨부 파일이 포함된 전자 메일 메시지를 만들고 보내는 방법을 보여 줍니다.

static void CreateMessageWithAttachment( String^ server )
{
   
   // Specify the file to be attached and sent.
   // This example assumes that a file named Data.xls exists in the
   // current working directory.
   String^ file = L"data.xls";
   
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"jane@contoso.com",L"ben@contoso.com",L"Quarterly data report.",L"See the attached spreadsheet." );
   
   // Create  the file attachment for this email message.
   Attachment^ data = gcnew Attachment(file, MediaTypeNames::Application::Octet);
   
   // Add time stamp information for the file.
   ContentDisposition^ disposition = data->ContentDisposition;
   disposition->CreationDate = System::IO::File::GetCreationTime( file );
   disposition->ModificationDate = System::IO::File::GetLastWriteTime( file );
   disposition->ReadDate = System::IO::File::GetLastAccessTime( file );
   
   // Add the file attachment to this email message.
   message->Attachments->Add( data );
   
   //Send the message.
   SmtpClient^ client = gcnew SmtpClient( server );
   
   // Add credentials if the SMTP server requires them.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   client->Send( message );
   
   // Display the values in the ContentDisposition for the attachment.
   ContentDisposition^ cd = data->ContentDisposition;
   Console::WriteLine( L"Content disposition" );
   Console::WriteLine( cd );
   Console::WriteLine( L"File {0}", cd->FileName );
   Console::WriteLine( L"Size {0}", cd->Size );
   Console::WriteLine( L"Creation {0}", cd->CreationDate );
   Console::WriteLine( L"Modification {0}", cd->ModificationDate );
   Console::WriteLine( L"Read {0}", cd->ReadDate );
   Console::WriteLine( L"Inline {0}", cd->Inline );
   Console::WriteLine( L"Parameters: {0}", cd->Parameters->Count );
   IEnumerator^ myEnum1 = cd->Parameters->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      DictionaryEntry^ d = safe_cast<DictionaryEntry^>(myEnum1->Current);
      Console::WriteLine( L"{0} = {1}", d->Key, d->Value );
   }

   data->~Attachment();
   client->~SmtpClient();
}
public static void CreateMessageWithAttachment(string server)
{
    // Specify the file to be attached and sent.
    // This example assumes that a file named Data.xls exists in the
    // current working directory.
    string file = "data.xls";
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
        "jane@contoso.com",
        "ben@contoso.com",
        "Quarterly data report.",
        "See the attached spreadsheet.");

    // Create  the file attachment for this email message.
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
    // Add time stamp information for the file.
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
    // Add the file attachment to this email message.
    message.Attachments.Add(data);

    //Send the message.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
            ex.ToString());
    }
    // Display the values in the ContentDisposition for the attachment.
    ContentDisposition cd = data.ContentDisposition;
    Console.WriteLine("Content disposition");
    Console.WriteLine(cd.ToString());
    Console.WriteLine("File {0}", cd.FileName);
    Console.WriteLine("Size {0}", cd.Size);
    Console.WriteLine("Creation {0}", cd.CreationDate);
    Console.WriteLine("Modification {0}", cd.ModificationDate);
    Console.WriteLine("Read {0}", cd.ReadDate);
    Console.WriteLine("Inline {0}", cd.Inline);
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
    foreach (DictionaryEntry d in cd.Parameters)
    {
        Console.WriteLine("{0} = {1}", d.Key, d.Value);
    }
    data.Dispose();
}
Public Shared Sub CreateMessageWithAttachment(ByVal server As String)
    ' Specify the file to be attached And sent.
    ' This example assumes that a file named Data.xls exists in the
    ' current working directory.
    Dim file As String = "data.xls"
    ' Create a message and set up the recipients.
    Dim message As MailMessage = New MailMessage(
        "jane@contoso.com",
        "ben@contoso.com",
        "Quarterly data report.",
        "See the attached spreadsheet.")

    ' Create  the file attachment for this email message.
    Dim data As Attachment = New Attachment(file, MediaTypeNames.Application.Octet)
    ' Add time stamp information for the file.
    Dim disposition As ContentDisposition = data.ContentDisposition
    disposition.CreationDate = System.IO.File.GetCreationTime(file)
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file)
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file)
    ' Add the file attachment to this email message.
    message.Attachments.Add(data)

    ' Send the message
    Dim client As SmtpClient = New SmtpClient(server)
    ' Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials

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

    ' Display the values in the ContentDisposition for the attachment.
    Dim cd As ContentDisposition = data.ContentDisposition
    Console.WriteLine("Content disposition")
    Console.WriteLine(cd.ToString())
    Console.WriteLine("File {0}", cd.FileName)
    Console.WriteLine("Size {0}", cd.Size)
    Console.WriteLine("Creation {0}", cd.CreationDate)
    Console.WriteLine("Modification {0}", cd.ModificationDate)
    Console.WriteLine("Read {0}", cd.ReadDate)
    Console.WriteLine("Inline {0}", cd.Inline)
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count)

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

    data.Dispose()
End Sub

설명

클래스의 MailMessage 인스턴스는 클래스를 사용하여 SmtpClient 배달을 위해 SMTP 서버로 전송되는 전자 메일 메시지를 생성하는 데 사용됩니다.

전자 메일 메시지의 보낸 사람, 받는 사람, 제목 및 본문은 가 개체를 초기화하는 MailMessage 데 사용될 때 MailMessage 매개 변수로 지정할 수 있습니다. 이러한 매개 변수는 개체의 MailMessage 속성을 사용하여 설정하거나 액세스할 수도 있습니다.

메시지의 기본 메일 메시지 헤더 및 요소는 클래스의 MailMessage 다음 속성을 사용하여 설정할 수 있습니다.

메일 머리글 또는 부분 속성
Attachments Attachments
BCC(블라인드 카본) Bcc
CC(탄소 복사본) CC
콘텐츠 형식 BodyEncoding
사용자 지정 헤더에 대한 인코딩 HeadersEncoding
메시지 본문 Body
우선 순위 Priority
받는 사람 To
Reply-To ReplyToList
보낸 사람 From
주체 Subject

합니다 MailMessage 클래스에서는 사용 하 여 메시지 헤더 컬렉션에 액세스 하려면 애플리케이션을 Headers 속성입니다. 이 컬렉션은 읽기 전용이지만(새 컬렉션을 설정할 수 없음) 이 컬렉션에서 사용자 지정 헤더를 추가하거나 삭제할 수 있습니다. 추가된 모든 사용자 지정 헤더는 instance 전송될 때 MailMessage 포함됩니다. 메시지를 보내기 전에 속성에서 이 컬렉션 Headers 에 특별히 추가된 헤더만 컬렉션에 포함됩니다. MailMessage instance 보낸 Headers 후 속성에는 클래스의 MailMessage 연결된 속성을 사용하여 설정된 헤더 또는 가 개체를 초기화하는 MailMessage 데 사용될 때 MailMessage 전달되는 매개 변수도 포함됩니다.

일부 메일 머리글의 형식이 잘못된 경우 메일 메시지가 손상될 수 있습니다. 따라서 클래스의 속성을 사용하여 설정할 수 있는 헤더 컬렉션의 MailMessage 모든 메일 헤더는 클래스 속성을 사용 MailMessage 하거나 개체를 초기화 MailMessage 할 때 MailMessage 전달된 매개 변수로만 설정해야 합니다. 다음 메일 머리글 목록은 속성을 사용하여 Headers 추가하면 안 되며, 메시지를 보낼 때 속성을 사용하여 Headers 이러한 헤더에 대해 설정된 값은 삭제되거나 덮어쓰여집니다.

  • Bcc

  • Cc

  • Content-ID

  • 콘텐츠-위치

  • Content-Transfer-Encoding

  • 콘텐츠 형식

  • 날짜

  • 시작

  • 중요도

  • MIME-Version

  • 우선 순위

  • Reply-To

  • 보낸 사람

  • 대상

  • X 우선 순위

애플리케이션이 사용 하 여 X-보낸 사람 헤더를 지정 하지 않은 경우는 Headers 속성을 MailMessage 클래스 만들어집니다 메시지를 보낼 때.

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

사용 된 Attachments 전자 메일 메시지에 첨부 파일을 추가 하는 속성입니다. 첨부 파일을 사용하여 메시지를 만드는 방법을 보여 주는 예제는 를 참조하세요 Attachments. MailMessage에서 Dispose를 호출하면 참조된 각 첨부 파일에서도 Dispose를 호출합니다.

전자 메일 메시지를 어셈블한 후 또는 SendAsync 메서드를 Send 사용하여 보낼 수 있습니다.

생성자

MailMessage()

MailMessage 클래스의 빈 인스턴스를 초기화합니다.

MailMessage(MailAddress, MailAddress)

지정된 MailMessage 클래스 개체를 사용하여 MailAddress 클래스의 새 인스턴스를 초기화합니다.

MailMessage(String, String)

지정된 MailMessage 클래스 개체를 사용하여 String 클래스의 새 인스턴스를 초기화합니다.

MailMessage(String, String, String, String)

MailMessage 클래스의 새 인스턴스를 초기화합니다.

속성

AlternateViews

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

Attachments

이 이메일 메시지에 첨부된 데이터를 저장하는 데 사용되는 첨부 파일 컬렉션을 가져옵니다.

Bcc

이 이메일 메시지의 BCC(숨은 참조) 받는 사람이 들어 있는 주소 컬렉션을 가져옵니다.

Body

메시지 본문을 가져오거나 설정합니다.

BodyEncoding

메시지 본문을 인코딩하는 데 사용되는 인코딩을 가져오거나 설정합니다.

BodyTransferEncoding

메시지 본문을 인코딩하는 데 사용되는 전송 인코딩을 가져오거나 설정합니다.

CC

이 이메일 메시지의 CC(참조) 받는 사람이 들어 있는 주소 컬렉션을 가져옵니다.

DeliveryNotificationOptions

이 이메일 메시지의 배달 알림을 가져오거나 설정합니다.

From

이 이메일 메시지의 보낸 사람 주소를 가져오거나 설정합니다.

Headers

이 이메일 메시지와 함께 전송되는 이메일 헤더를 가져옵니다.

HeadersEncoding

이 이메일 메시지의 사용자 정의된 사용자 지정 헤더에 사용되는 인코딩을 가져오거나 설정합니다.

IsBodyHtml

전자 메일 메시지 본문의 형식이 HTML인지 여부를 나타내는 값을 가져오거나 설정합니다.

Priority

이 이메일 메시지의 우선 순위를 가져오거나 설정합니다.

ReplyTo
사용되지 않음.
사용되지 않음.
사용되지 않음.

전자 메일 메시지의 회신 주소를 가져오거나 설정합니다.

ReplyToList

메일 메시지의 회신 주소 목록을 가져옵니다.

Sender

이 이메일 메시지의 보낸 사람 주소를 가져오거나 설정합니다.

Subject

이 이메일 메시지의 제목 줄을 가져오거나 설정합니다.

SubjectEncoding

이 이메일 메시지의 제목 내용에 사용되는 인코딩을 가져오거나 설정합니다.

To

이 이메일 메시지의 받는 사람이 들어 있는 주소 컬렉션을 가져옵니다.

메서드

Dispose()

MailMessage에서 사용하는 모든 리소스를 해제합니다.

Dispose(Boolean)

MailMessage에서 사용하는 관리되지 않는 리소스를 해제하고, 관리되는 리소스를 선택적으로 해제할 수 있습니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상