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 實例可用來建構傳送至 SMTP 伺服器的電子郵件訊息,以便使用 SmtpClient 類別進行傳遞。

使用 初始化 物件時 MailMessage ,可以指定電子郵件訊息的寄件者、收件者、主旨和本文做 MailMessage 為參數。 您也可以使用物件上的 MailMessage 屬性來設定或存取這些參數。

您可以使用 類別的下列屬性 MailMessage 來設定郵件的主要郵件訊息標頭和元素。

郵件標頭或元件 屬性
附件 Attachments
密件抄送副本 (BCC) Bcc
副本副本 (CC) CC
Content-Type BodyEncoding
自定義標頭的編碼 HeadersEncoding
訊息內文 Body
優先順序 Priority
收件者 To
Reply-To ReplyToList
傳送者 From
主體 Subject

類別 MailMessage 也允許應用程式使用 Headers 屬性來存取訊息的標頭集合。 雖然這個集合是只讀的 (無法設定新的集合) ,但自定義標頭可以新增至或從這個集合中刪除。 傳送實例時 MailMessage ,將會包含新增的任何自定義標頭。 傳送訊息之前,只會在 集合中包含特別新增至這個集合的 Headers 標頭。 MailMessage傳送實例之後,屬性也會包含標頭,Headers這些標頭是使用 用來初始化 MailMessage 物件時MailMessage所傳遞類別MailMessage或參數的相關屬性所設定。

如果某些郵件標頭格式不正確,可能會導致電子郵件訊息損毀。 因此,可以在類別上使用 MailMessage 屬性設定之標頭集合中的任何郵件標頭,應該只使用 MailMessage 類別屬性或初始化 MailMessage 物件時MailMessage傳遞的參數來設定。 不應使用 屬性來新增下列郵件標頭清單, Headers 而且當傳送郵件時,將會捨棄或覆寫使用這些 Headers 標頭設定的任何值:

  • 密件副本

  • 副本

  • Content-ID

  • 內容位置

  • Content-Transfer-Encoding

  • Content-Type

  • Date

  • 寄件者

  • 重要性

  • MIME-Version

  • 優先順序

  • Reply-To

  • 傳送者

  • 收件者

  • X 優先順序

如果應用程式未使用 Headers 屬性指定 X-Sender 標頭,類別 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
已淘汰.
已淘汰.
已淘汰.

取得或設定郵件訊息的 ReplyTo 地址。

ReplyToList

取得郵件訊息回覆的地址清單。

Sender

取得或設定這個電子郵件訊息的寄件者地址。

Subject

取得或設定這個電子郵件訊息的主旨。

SubjectEncoding

取得或設定這個電子郵件訊息之主旨內容所用的編碼方式。

To

取得地址集合,其中包含這個電子郵件訊息的收件者。

方法

Dispose()

釋放 MailMessage 所使用的所有資源。

Dispose(Boolean)

釋放 MailMessage 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於