MailAddress 類別

定義

表示電子郵件寄件者或收件者的地址。

public ref class MailAddress
public class MailAddress
type MailAddress = class
Public Class MailAddress
繼承
MailAddress

範例

下列程式碼範例示範如何使用 SmtpClientMailAddressMailMessage 類別傳送電子郵件訊息。

static void CreateCopyMessage( 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 = "Using the SmtpClient class.";
   message->Subject = L"Using the SmtpClient class.";
   message->Body = L"Using this feature, you can send an email message from an application very easily.";
   
   // Add a carbon copy recipient.
   MailAddress^ copy = gcnew MailAddress( L"Notification_List@contoso.com" );
   message->CC->Add( copy );
   SmtpClient^ client = gcnew SmtpClient( server );
   
   // Include credentials if the server requires them.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   Console::WriteLine( L"Sending an email message to {0} by using the SMTP host {1}.", to->Address, client->Host );
   client->Send( message );
   client->~SmtpClient();
}
public static void CreateCopyMessage(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.Subject = "Using the SmtpClient class.";
    message.Body = @"Using this feature, you can send an email message from an application very easily.";
    // Add a carbon copy recipient.
    MailAddress copy = new MailAddress("Notification_List@contoso.com");
    message.CC.Add(copy);
    SmtpClient client = new SmtpClient(server);
    // Include credentials if the server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    Console.WriteLine("Sending an email message to {0} by using the SMTP host {1}.",
         to.Address, client.Host);

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateCopyMessage(): {0}",
            ex.ToString());
    }
}

備註

MailMessage 類別會使用 SmtpClient 類別 MailAddress 來儲存電子郵件訊息的位址資訊。

電子郵件地址是由 User 名稱、 Host 名稱以及選擇性的 。 DisplayName DisplayName如果您將其編碼,則可以包含非 ASCII 字元。

類別 MailAddress 支援下列郵寄地址格式:

  • user@host 簡單位址格式。 DisplayName如果未設定 ,則這是產生的郵寄地址格式。

  • 的標準引號顯示名稱格式 "display name" <user@host>DisplayName如果 已設定 ,則這是產生的格式。

  • 如果未包含這些括弧,則會在名稱和 Host 名稱周圍 User 加上角括弧。 例如, "display name" user@host 會變更為 "display name" <user@host>

  • 如果未包含引號, DisplayName 則會在 周圍加上引號。 例如, display name <user@host> 會變更為 "display name" <user@host>

  • 屬性支援 DisplayName Unicode 字元。

  • User具有引號的名稱。 例如: "user name"@host

  • 使用者名稱中的連續和尾端點。 例如: user...name..@host

  • 方括弧定義域常值。 例如: <user@[my domain]>

  • 註解。 例如: (comment)"display name"(comment)<(comment)user(comment)@(comment)domain(comment)>(comment) 。 傳輸之前會移除批註。

逗號用來分隔郵寄地址清單中的專案。 因此,在清單中未加上批註的顯示名稱中不應使用逗號。 允許下列郵寄地址:

"John, Doe" <user@host>, "Bob, Smith" <user2@host>

不允許下列郵寄地址:

John, Doe <user@host>, Bob, Smith <user2@host>

引號可以內嵌在引號字串中,但必須逸出。 允許下列郵寄地址:

"John \"Jr\" Doe" <user@host>

"\"John \\\"Jr\\\" Doe\" <user@host>"

不允許下列郵寄地址:

"John "Jr" Doe" <user@host>

未加上引號時,字串開頭 (或逗號) 和位址之間的所有文字都會被視為 的 DisplayName 一部分,包括批註。 例如:

(non comment) unquoted display (non comment) name (non comment) <user@host>

雖然 類別 MailAddress 接受郵寄地址為有效,但其他郵件伺服器可能不接受郵寄地址。

類別 MailAddress 不支援下列郵寄地址格式:

  • 混合加上引號和未加上引號的顯示名稱。 例如: display "name" <user@host>

  • 群組,如 IETF 所發佈的 RFC 2822 第 3.4 節中所定義。

  • 或 的過時使用者名稱格式 "user"."name"@host user."name"@host "user".name@host

建構函式

MailAddress(String)

使用指定的位址將新的 MailAddress 執行個體初始化。

MailAddress(String, String)

使用指定的位址及顯示名稱將新的 MailAddress 執行個體初始化。

MailAddress(String, String, Encoding)

使用指定的地址、顯示名稱和編碼方式,初始化 MailAddress 類別的新執行個體。

屬性

Address

取得建立這個執行個體時所指定的電子郵件地址。

DisplayName

取得顯示名稱,該名稱是由建立這個執行個體時所指定的顯示名稱和地址資訊所組成。

Host

取得建立這個執行個體時所指定之地址的主機部分。

User

取得建立這個執行個體時所指定之地址的使用者資訊。

方法

Equals(Object)

比較兩個郵件地址。

GetHashCode()

傳回郵件地址的雜湊值 (Hash Value)。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回這個執行個體的字串表示。

TryCreate(String, MailAddress)

建立新的 MailAddress。 如果無法建立物件,則不會擲回例外狀況。

TryCreate(String, String, Encoding, MailAddress)

建立新的 MailAddress。 如果無法建立物件,則不會擲回例外狀況。

TryCreate(String, String, MailAddress)

建立新的 MailAddress。 如果無法建立物件,則不會擲回例外狀況。

適用於