Edit

Share via


MailAddress.ToString Method

Definition

Returns a string representation of this instance.

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

Returns

A String that contains the contents of this MailAddress.

Examples

The following code example displays the destination of an email message.

static void CreateTestMessage4( String^ server )
{
   MailAddress^ from = gcnew MailAddress( L"[email protected]" );
   MailAddress^ to = gcnew MailAddress( L"[email protected]" );
   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.";
   SmtpClient^ client = gcnew SmtpClient( server );
   Console::WriteLine( L"Sending an email message to {0} by using SMTP host {1} port {2}.", to, client->Host, client->Port );
   client->Send( message );
   client->~SmtpClient();
}
public static void CreateTestMessage4(string server)
{
    MailAddress from = new MailAddress("[email protected]");
    MailAddress to = new MailAddress("[email protected]");
    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());
    }
}

Remarks

The string returned by this method is not necessarily identical to the string specified when the instance was created. If the DisplayName property is not null and not equal to String.Empty (""), ToString returns a string composed using the following syntax:

DisplayName <User@Host>

If DisplayName is null or is equal to String.Empty, ToString returns the value of the Address property.

Applies to