MailAddress 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 MailAddress 類別的新執行個體。
多載
MailAddress(String) |
使用指定的位址將新的 MailAddress 執行個體初始化。 |
MailAddress(String, String) |
使用指定的位址及顯示名稱將新的 MailAddress 執行個體初始化。 |
MailAddress(String, String, Encoding) |
使用指定的地址、顯示名稱和編碼方式,初始化 MailAddress 類別的新執行個體。 |
MailAddress(String)
使用指定的位址將新的 MailAddress 執行個體初始化。
public:
MailAddress(System::String ^ address);
public MailAddress (string address);
new System.Net.Mail.MailAddress : string -> System.Net.Mail.MailAddress
Public Sub New (address As String)
參數
例外狀況
address
為 null
。
address
為 Empty ("")。
address
的格式無法辨識。
範例
下列程式代碼範例會使用此建構函式來建立 MailAddress 電子郵件訊息收件者的物件 Bcc 。
static void CreateBccTestMessage( String^ server )
{
MailAddress^ from = gcnew MailAddress( L"ben@contoso.com",L"Ben Miller" );
MailAddress^ to = gcnew MailAddress( L"jane@contoso.com",L"Jane Clayton" );
MailMessage^ message = gcnew MailMessage( from,to );
message->Subject = L"Using the SmtpClient class.";
message->Body = L"Using this feature, you can send an email message from an application very easily.";
MailAddress^ bcc = gcnew MailAddress( L"manager1@contoso.com" );
message->Bcc->Add( bcc );
SmtpClient^ client = gcnew SmtpClient( server );
client->Credentials = CredentialCache::DefaultNetworkCredentials;
Console::WriteLine( L"Sending an email message to {0} and {1}.", to->DisplayName, message->Bcc );
try
{
client->Send( message );
}
catch ( Exception^ ex )
{
Console::WriteLine(L"Exception caught in CreateBccTestMessage(): {0}",
ex->ToString() );
}
client->~SmtpClient();
}
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = @"Using this feature, you can send an email message from an application very easily.";
MailAddress bcc = new MailAddress("manager1@contoso.com");
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an email message to {0} and {1}.",
to.DisplayName, message.Bcc.ToString());
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString());
}
}
備註
如果您以角括弧括住地址,參數 address
可以包含顯示名稱和相關聯的電子郵件位址。 例如:
"Tom Smith <tsmith@contoso.com>"
顯示名稱和角括弧之間允許空格符。
下表顯示使用上述範例位址建構之 物件的屬性值 MailAddress 。
屬性 | 值 |
---|---|
DisplayName | "Tom Smith" |
Host | "contoso.com" |
User | "tsmith" |
Address | "tsmith@contoso.com" |
適用於
MailAddress(String, String)
使用指定的位址及顯示名稱將新的 MailAddress 執行個體初始化。
public:
MailAddress(System::String ^ address, System::String ^ displayName);
public MailAddress (string address, string? displayName);
public MailAddress (string address, string displayName);
new System.Net.Mail.MailAddress : string * string -> System.Net.Mail.MailAddress
Public Sub New (address As String, displayName As String)
參數
例外狀況
address
為 null
。
address
為 Empty ("")。
範例
下列程式代碼範例會使用此建構函式來建立 MailAddress 電子郵件訊息發件人和收件者的實例。
static void CreateBccTestMessage( String^ server )
{
MailAddress^ from = gcnew MailAddress( L"ben@contoso.com",L"Ben Miller" );
MailAddress^ to = gcnew MailAddress( L"jane@contoso.com",L"Jane Clayton" );
MailMessage^ message = gcnew MailMessage( from,to );
message->Subject = L"Using the SmtpClient class.";
message->Body = L"Using this feature, you can send an email message from an application very easily.";
MailAddress^ bcc = gcnew MailAddress( L"manager1@contoso.com" );
message->Bcc->Add( bcc );
SmtpClient^ client = gcnew SmtpClient( server );
client->Credentials = CredentialCache::DefaultNetworkCredentials;
Console::WriteLine( L"Sending an email message to {0} and {1}.", to->DisplayName, message->Bcc );
try
{
client->Send( message );
}
catch ( Exception^ ex )
{
Console::WriteLine(L"Exception caught in CreateBccTestMessage(): {0}",
ex->ToString() );
}
client->~SmtpClient();
}
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = @"Using this feature, you can send an email message from an application very easily.";
MailAddress bcc = new MailAddress("manager1@contoso.com");
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an email message to {0} and {1}.",
to.DisplayName, message.Bcc.ToString());
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString());
}
}
備註
保留顯示名稱中的前置和尾端空格符。
如果 displayName
包含非 ASCII 字元,則會使用 iso-8859-1 字元集進行 displayName
編碼。 RFC 1522 中會討論編碼非 ASCII 字元,可在 取得 https://www.ietf.org/。
如果 address
包含顯示名稱,且 displayName
不 null
等於 String.Empty, displayName
則會覆寫 中指定的 address
值。
適用於
MailAddress(String, String, Encoding)
使用指定的地址、顯示名稱和編碼方式,初始化 MailAddress 類別的新執行個體。
public:
MailAddress(System::String ^ address, System::String ^ displayName, System::Text::Encoding ^ displayNameEncoding);
public MailAddress (string address, string? displayName, System.Text.Encoding? displayNameEncoding);
public MailAddress (string address, string displayName, System.Text.Encoding displayNameEncoding);
new System.Net.Mail.MailAddress : string * string * System.Text.Encoding -> System.Net.Mail.MailAddress
Public Sub New (address As String, displayName As String, displayNameEncoding As Encoding)
參數
例外狀況
範例
下列程式代碼範例會使用此建構函式來建立 MailAddress 電子郵件訊息發件人的實例。
// Create a mailing address that includes a UTF8
// character in the display name.
MailAddress^ from = gcnew MailAddress("jane@contoso.com",
"Jane " + (wchar_t)0xD8 + " Clayton",
System::Text::Encoding::UTF8);
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("jane@contoso.com",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
' Create a mailing address that includes a UTF8 character
' in the display name.
Dim mailFrom As New MailAddress("jane@contoso.com", "Jane " & ChrW(&HD8) & " Clayton", System.Text.Encoding.UTF8)
備註
保留顯示名稱中的前置和尾端空格符。
如果 address
包含顯示名稱,且 displayName
不 null
等於 String.Empty, displayName
則會覆寫 中指定的 address
值。
方法 MailAddress 不會檢查參數是否 displayName
有效。 這個方法會移除 屬性未顯示的 DisplayName 周圍引號。 在傳輸之前會新增引號,但 參數中displayNameEncoding
指定 或 Unicode 的位置除外ASCII。 在傳輸ASCII之前,或 Unicode 是在 參數中displayNameEncoding
指定之前,將參數中指定的displayNameEncoding
編碼套用至 DisplayName 屬性。 UTF8 如果未指定,則為預設編碼方式。
如您支援之電子郵件地址格式的詳細資訊,請參閱 MailAddress。