MailMessage.Headers MailMessage.Headers MailMessage.Headers MailMessage.Headers Property

定義

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

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

屬性值

範例

下列程式碼範例示範如何顯示郵件訊息的標頭。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"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();
}

備註

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. 當傳送實例時, 將會包含任何MailMessage新增的自訂標頭。Any custom headers added will be included when the MailMessage instance is sent. 在傳送訊息之前, 集合中只會包含特別在Headers屬性中新增至這個集合的標頭。Before a message is sent, only headers specifically added to this collection in the Headers property are included in the collection. MailMessage傳送實例之後, 此Headers屬性也會包含標頭, 其會使用MailMessage類別的相關聯屬性來設定, 或當用MailMessage來初始化MailMessage物件時所傳遞的參數。After 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初始化MailMessage物件時所傳遞的參數。So 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. 下列的郵件頭清單不應使用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

  • CcCc

  • 內容識別碼Content-ID

  • Content-LocationContent-Location

  • Content-Transfer-EncodingContent-Transfer-Encoding

  • Content-TypeContent-Type

  • 日期Date

  • From

  • 重要性Importance

  • MIME 版本MIME-Version

  • 優先權Priority

  • 回復Reply-To

  • 發送者Sender

  • To

  • X-優先順序X-Priority

如果應用程式未使用Headers屬性指定 X 傳送者標頭, 則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 MailMessage初始化物件時, 可能會將電子郵件訊息的寄件者、收件者、主旨和本文指定為參數。The sender, recipient, subject, and body of an email message may be specified as parameters when a MailMessage is used to initialize a MailMessage object. 您也可以使用物件上的MailMessage屬性來設定或存取這些參數。These parameters may also be set or accessed using properties on the MailMessage object.

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

郵件標頭或部分Mail header or part 屬性Property
附件Attachments Attachments
密件副本 (BCC)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-To ReplyToList
發送者Sender From
主旨Subject Subject

適用於