Bagikan melalui


MailMessage.Headers Properti

Definisi

Mendapatkan header email yang dikirimkan dengan pesan email ini.

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

Nilai Properti

NameValueCollection yang berisi header email.

Contoh

Contoh kode berikut menunjukkan menampilkan header untuk pesan email.

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

Keterangan

Properti Headers memungkinkan aplikasi mengakses koleksi header untuk pesan. Meskipun koleksi ini baca-saja (koleksi baru tidak dapat diatur), header kustom dapat ditambahkan ke atau dihapus dari koleksi ini. Header kustom apa pun yang ditambahkan akan disertakan saat MailMessage instans dikirim. Sebelum pesan dikirim, hanya header yang secara khusus ditambahkan ke koleksi ini di Headers properti yang disertakan dalam koleksi. MailMessage Setelah instans dikirim, Headers properti juga akan menyertakan header yang diatur menggunakan properti terkait dari MailMessage kelas atau parameter yang MailMessage dilewatkan saat digunakan untuk menginisialisasi MailMessage objek.

Jika beberapa header email cacat, mereka dapat menyebabkan pesan email rusak. Jadi header email apa pun di koleksi header yang dapat diatur menggunakan properti pada MailMessage kelas hanya boleh diatur menggunakan MailMessage properti kelas atau sebagai parameter yang MailMessage diteruskan saat menginisialisasi MailMessage objek. Daftar header email berikut tidak boleh ditambahkan menggunakan Headers properti dan nilai apa pun yang ditetapkan untuk header ini menggunakan Headers properti akan dibuang atau ditimpa saat pesan dikirim:

  • Bcc

  • Cc

  • CONTENT-ID

  • Lokasi Konten

  • Pengodean Transfer Konten

  • Jenis-Konten

  • Tanggal

  • Dari

  • Pentingnya

  • MIME-Version

  • Prioritas

  • Reply-To

  • Pengirim

  • Untuk

  • Prioritas X

Jika aplikasi tidak menentukan header X-Sender menggunakan Headers properti , MailMessage kelas akan membuatnya saat pesan dikirim.

Pengirim, penerima, subjek, dan isi pesan email dapat ditentukan sebagai parameter saat MailMessage digunakan untuk menginisialisasi MailMessage objek. Parameter ini juga dapat diatur atau diakses menggunakan properti pada MailMessage objek .

Header dan elemen pesan email utama untuk pesan dapat diatur menggunakan properti MailMessage kelas berikut.

Header atau bagian surat Properti
Lampiran Attachments
Salinan karbon buta (BCC) Bcc
Salinan karbon (CC) CC
Jenis-Konten BodyEncoding
Pengodean untuk header kustom HeadersEncoding
Isi pesan Body
Prioritas Priority
Penerima To
Reply-To ReplyToList
Pengirim From
Subjek Subject

Berlaku untuk