MailMessage.Headers 屬性


取得與這個電子郵件訊息一起傳送的電子郵件標頭。Gets the email headers that are transmitted with this email message.

 property System::Collections::Specialized::NameValueCollection ^ Headers { System::Collections::Specialized::NameValueCollection ^ get(); };
public System.Collections.Specialized.NameValueCollection Headers { get; }
member this.Headers : System.Collections.Specialized.NameValueCollection
Public ReadOnly Property Headers As NameValueCollection



NameValueCollection包含電子郵件標題的。A NameValueCollection that contains the email headers.


下列程式碼範例示範如何顯示郵件訊息的標頭。The following code example demonstrates displaying the headers for a mail message.

static void CreateMessageWithAttachment4( String^ server, String^ to )
   // Specify the file to be attached and sent.
   // This example uses a file on a UNC share.
   String^ file = L"\\\\share3\\c$\\reports\\data.xls";
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"",to,L"Quarterly data report",L"See the attached spreadsheet." );
   // Create  the file attachment for this email message.
   Attachment^ data = gcnew Attachment("qtr3.xls", 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 );
   disposition->DispositionType = DispositionTypeNames::Attachment;
   // 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 = dynamic_cast<ICredentialsByHost^>(CredentialCache::DefaultNetworkCredentials);
   client->Send( message );
   // Display the message headers.
   array<String^>^keys = message->Headers->AllKeys;
   Console::WriteLine( L"Headers" );
   IEnumerator^ myEnum3 = keys->GetEnumerator();
   while ( myEnum3->MoveNext() )
      String^ s = safe_cast<String^>(myEnum3->Current);
      Console::WriteLine( L"{0}:", s );
      Console::WriteLine( L"    {0}", message->Headers[ s ] );


public static void CreateMessageWithAttachment4(string server, string to)
    // Specify the file to be attached and sent.
    // This example uses a file on a UNC share.
    string file = @"\\share3\c$\reports\data.xls";
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
       "Quarterly data report",
       "See the attached spreadsheet.");

    // Create  the file attachment for this email message.
    Attachment data = new Attachment("qtr3.xls", 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);
    disposition.DispositionType = DispositionTypeNames.Attachment;
    // Add the file attachment to this email message.
    //Send the message.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;
    // Display the message headers.
    string[] keys = message.Headers.AllKeys;
    foreach (string s in keys)
        Console.WriteLine("{0}:", s);
        Console.WriteLine("    {0}", message.Headers[s]);
Public Shared Sub CreateMessageWithAttachment4(ByVal server As String, ByVal [to] As String)
    ' Specify the file to be attached And sent.
    ' This example uses a file on a UNC share.
    Dim file As String = "\\share3\c$\reports\data.xls"
    Dim message As MailMessage = New MailMessage("", [to], "Quarterly data report", "See the attached spreadsheet.")
    ' Create  the file attachment for this email message.
    Dim data As Attachment = New Attachment("qtr3.xls", 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)
    disposition.DispositionType = DispositionTypeNames.Attachment
    ' Add the file attachment to this email message.
    'Send the message.
    Dim client As SmtpClient = New SmtpClient(server)
    ' Add credentials if the SMTP server requires them.
    client.Credentials = CType(CredentialCache.DefaultNetworkCredentials, ICredentialsByHost)
    ' Display the message headers.
    Dim keys As String() = message.Headers.AllKeys

    For Each s As String In keys
        Console.WriteLine("{0}:", s)
        Console.WriteLine("    {0}", message.Headers(s))

End Sub


Headers屬性可讓應用程式存取訊息的標頭集合。The Headers property allows an application to access the headers collection for the message. 雖然此集合是唯讀的 (但無法設定新的集合) ,您可以在此集合中加入或刪除自訂標頭。While this collection is read-only (a new collection can not be set), custom headers can be added to or deleted from this collection. 傳送實例時,將會包含任何新增的自訂標頭 MailMessageAny custom headers added will be included when the MailMessage instance is sent. 傳送訊息之前,只會在此集合中包含特別加入此集合中的標頭 HeadersBefore a message is sent, only headers specifically added to this collection in the Headers property are included in the collection. MailMessage傳送實例之後, Headers 屬性也會包含使用類別的相關聯屬性設定的標頭,或在 MailMessage MailMessage 用來初始化物件時傳遞的參數 MailMessageAfter the MailMessage instance is sent, the Headers property will also include headers that are set using the associated properties of the MailMessage class or parameters passed when a MailMessage is used to initialize a MailMessage object.

如果某些郵件標頭的格式不正確,可能會導致電子郵件訊息損毀。If some mail headers are malformed, they could cause the email message to become corrupted. 因此,可以使用類別上的屬性設定的標頭集合中的任何郵件標頭, MailMessage 只應使用類別屬性來設定, MailMessage 或是在初始化物件時,以傳遞的參數來設定 MailMessage MailMessageSo any mail header in the headers collection that can be set using a property on the MailMessage class should only be set using the MailMessage class property or as a parameter passed when a MailMessage initializes a MailMessage object. 下列 mail 標頭清單不應使用 Headers 屬性加入,而且在 Headers 傳送訊息時,將會捨棄或覆寫這些標頭使用屬性設定的任何值:The following list of mail headers should not be added using the Headers property and any values set for these headers using the Headers property will be discarded or overwritten when the message is sent:

  • 密件副本Bcc

  • 副本Cc

  • 內容識別碼Content-ID

  • 內容位置Content-Location

  • 內容-傳輸-編碼Content-Transfer-Encoding

  • Content-TypeContent-Type

  • DateDate

  • 寄件者From

  • 重要性Importance

  • MIME-VersionMIME-Version

  • 優先順序Priority

  • Reply-ToReply-To

  • 傳送者Sender

  • 收件者To

  • X 優先順序X-Priority

如果應用程式未使用屬性指定 X 寄件者標頭 Headers ,則 MailMessage 類別會在傳送訊息時建立一個標頭。If the application does not specify an X-Sender header using the Headers property, the MailMessage class will create one when the message is sent.

MailMessage 用來初始化物件時,電子郵件訊息的寄件者、收件者、主旨和本文可能會指定為參數 MailMessageThe sender, recipient, subject, and body of an email message may be specified as parameters when a MailMessage is used to initialize a MailMessage object. 您也可以使用物件上的屬性來設定或存取這些參數 MailMessageThese parameters may also be set or accessed using properties on the MailMessage object.

您可以使用類別的下列屬性來設定訊息的主要郵件訊息標頭和元素 MailMessageThe primary mail message headers and elements for the message may be set using the following properties of the MailMessage class.

Mail 頁首或 partMail header or part 屬性Property
附件Attachments Attachments
密件副本 (密件副本) Blind carbon copies (BCC) Bcc
副本 (CC) Carbon copies (CC) CC
Content-TypeContent-Type BodyEncoding
自訂標頭的編碼Encoding for custom headers HeadersEncoding
訊息內文Message body Body
優先順序Priority Priority
收件者Recipient To
Reply-ToReply-To ReplyToList
傳送者Sender From
主體Subject Subject