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-Location

  • Content-Transfer-Encoding

  • Content-Type

  • Date

  • 重要性

  • MIME-Version

  • 优先级

  • 答复

  • 发送方

  • 功能

  • X 优先级

如果应用程序未使用 Headers 属性指定 X-Sender 标头,则 MailMessage 类将在发送消息时创建一个标头。

使用 初始化 MailMessage 对象时MailMessage,可将电子邮件的发件人、收件人、主题和正文指定为参数。 还可以使用 对象上的属性设置或访问这些 MailMessage 参数。

可以使用 类的以下属性 MailMessage 设置邮件的主邮件邮件头和元素。

邮件头或部分 properties
Attachments Attachments
密件抄送 (密件抄送) Bcc
抄送 (CC) CC
Content-Type BodyEncoding
自定义标头的编码 HeadersEncoding
邮件正文 Body
优先级 Priority
Recipient To
答复 ReplyToList
发送方 From
主题 Subject

适用于