共用方式為


MailMessage.Headers 屬性

定義

取得與這個電子郵件訊息一起傳送的電子郵件標頭。

public:
 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,其中包含電子郵件標頭。

範例

下列程式代碼範例示範如何顯示郵件訊息的標頭。

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"ReportMailer@contoso.com",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 ] );
   }

   data->~Attachment();
   client->~SmtpClient();
}
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(
       "ReportMailer@contoso.com",
       to,
       "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.
    message.Attachments.Add(data);
    //Send the message.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;
    client.Send(message);
    // Display the message headers.
    string[] keys = message.Headers.AllKeys;
    Console.WriteLine("Headers");
    foreach (string s in keys)
    {
        Console.WriteLine("{0}:", s);
        Console.WriteLine("    {0}", message.Headers[s]);
    }
    data.Dispose();
}
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("ReportMailer@contoso.com", [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.
    message.Attachments.Add(data)
    'Send the message.
    Dim client As SmtpClient = New SmtpClient(server)
    ' Add credentials if the SMTP server requires them.
    client.Credentials = CType(CredentialCache.DefaultNetworkCredentials, ICredentialsByHost)
    client.Send(message)
    ' Display the message headers.
    Dim keys As String() = message.Headers.AllKeys
    Console.WriteLine("Headers")

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

    data.Dispose()
End Sub

備註

屬性 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 會在傳送訊息時建立一個標頭。

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

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

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

適用於