共用方式為


MailAddress 建構函式

定義

初始化 MailAddress 類別的新執行個體。

多載

MailAddress(String)

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

MailAddress(String, String)

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

MailAddress(String, String, Encoding)

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

MailAddress(String)

來源:
MailAddress.cs
來源:
MailAddress.cs
來源:
MailAddress.cs

使用指定的位址將新的 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
String

包含電子郵件地址的 String

例外狀況

addressnull

addressEmpty ("")。

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.cs
來源:
MailAddress.cs
來源:
MailAddress.cs

使用指定的位址及顯示名稱將新的 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
String

包含電子郵件地址的 String

displayName
String

String,包含與 address 相關聯的顯示名稱。 這個參數可以是 null

例外狀況

addressnull

addressEmpty ("")。

address 的格式無法辨識。

-或-

address 包含非 ASCII 字元。

範例

下列程式代碼範例會使用此建構函式來建立 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 包含顯示名稱,且 displayNamenull 等於 String.EmptydisplayName 則會覆寫 中指定的 address值。

適用於

MailAddress(String, String, Encoding)

來源:
MailAddress.cs
來源:
MailAddress.cs
來源:
MailAddress.cs

使用指定的地址、顯示名稱和編碼方式,初始化 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)

參數

address
String

包含電子郵件地址的 String

displayName
String

String,包含與 address 關聯的顯示名稱。

displayNameEncoding
Encoding

Encoding,其定義用於 displayName 的字元集。

例外狀況

addressnull

-或-

displayNamenull

addressEmpty ("")。

-或-

displayNameEmpty ("")。

address 的格式無法辨識。

-或-

address 包含非 ASCII 字元。

範例

下列程式代碼範例會使用此建構函式來建立 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 包含顯示名稱,且 displayNamenull 等於 String.EmptydisplayName 則會覆寫 中指定的 address值。

方法 MailAddress 不會檢查參數是否 displayName 有效。 這個方法會移除 屬性未顯示的 DisplayName 周圍引號。 在傳輸之前會新增引號,但 參數中displayNameEncoding指定 或 Unicode 的位置除外ASCII。 在傳輸ASCII之前,或 Unicode 是在 參數中displayNameEncoding指定之前,將參數中指定的displayNameEncoding編碼套用至 DisplayName 屬性。 UTF8 如果未指定,則為預設編碼方式。

如您支援之電子郵件地址格式的詳細資訊,請參閱 MailAddress

適用於