語言

MailAddress.ToString 方法

定義

回傳此實例的字串表示。

public:
 override System::String ^ ToString();
public override string ToString();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

String A 包含此MailAddress內容。

範例

以下程式碼範例顯示電子郵件的目的地。

public static void CreateTestMessage4(string server)
{
    MailAddress from = new MailAddress("ben@contoso.com");
    MailAddress to = new MailAddress("Jane@contoso.com");
    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.";
    SmtpClient client = new SmtpClient(server);
    Console.WriteLine("Sending an email message to {0} by using SMTP host {1} port {2}.",
         to.ToString(), client.Host, client.Port);

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

備註

此方法回傳的字串不一定與建立實例時指定的字串相同。 若屬性 DisplayNamenull 等於(“ String.Empty ”), ToString() 則回傳以下語法組成的字串:

DisplayName <User@Host>

DisplayNamenull 等於 String.EmptyToString() 則返回該 Address 性質的值。

適用於