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関連付けられたプロパティを使用して設定されたヘッダーも含まれます。

一部のメール ヘッダーの形式が正しくない場合は、メール メッセージが破損する可能性があります。 そのため、 クラスの プロパティを使用して設定できる headers コレクション内のメール ヘッダーは、 クラス プロパティMailMessageを使用するか、 がオブジェクトをMailMessage初期化MailMessageするときにMailMessage渡されるパラメーターとしてのみ設定する必要があります。 プロパティを使用して次のメール ヘッダーの一覧を Headers 追加しないでください。また、 プロパティを使用して Headers これらのヘッダーに設定された値は、メッセージの送信時に破棄または上書きされます。

  • [Bcc]

  • [Cc]

  • Content-ID

  • Content-Location

  • Content-Transfer-Encoding

  • Content-Type

  • Date

  • ソース

  • 重要度

  • MIME-Version

  • 優先度

  • [返信先]

  • 送信者

  • 終了

  • X 優先度

アプリケーションで プロパティを使用して X-Sender ヘッダーを Headers 指定しない場合、 MailMessage クラスはメッセージの送信時に X-Sender ヘッダーを作成します。

を使用してオブジェクトを初期化MailMessageする場合MailMessageは、電子メール メッセージの送信者、受信者、件名、本文をパラメーターとして指定できます。 これらのパラメーターは、 オブジェクトの MailMessage プロパティを使用して設定またはアクセスすることもできます。

メッセージのプライマリ メール メッセージ ヘッダーと要素は、 クラスの次の MailMessage プロパティを使用して設定できます。

メールヘッダーまたはパート プロパティ
[Attachments] Attachments
ブラインド カーボン コピー (BCC) Bcc
カーボン コピー (CC) CC
Content-Type BodyEncoding
カスタム ヘッダーのエンコード HeadersEncoding
メッセージ本文 Body
優先度 Priority
Recipient To
[返信先] ReplyToList
送信者 From
サブジェクト Subject

適用対象