Redigera

Dela via


MailAddress Constructors

Definition

Initializes a new instance of the MailAddress class.

Overloads

MailAddress(String)

Initializes a new instance of the MailAddress class using the specified address.

MailAddress(String, String)

Initializes a new instance of the MailAddress class using the specified address and display name.

MailAddress(String, String, Encoding)

Initializes a new instance of the MailAddress class using the specified address, display name, and encoding.

MailAddress(String)

Source:
MailAddress.cs
Source:
MailAddress.cs
Source:
MailAddress.cs

Initializes a new instance of the MailAddress class using the specified address.

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)

Parameters

address
String

A String that contains an email address.

Exceptions

address is null.

address is Empty ("").

address is not in a recognized format.

Examples

The following code example uses this constructor to create a MailAddress object for the Bcc recipient of an email message.

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());
    }
}

Remarks

The address parameter can contain a display name and the associated email address if you enclose the address in angle brackets. For example:

"Tom Smith <tsmith@contoso.com>"

White space is permitted between the display name and the angle brackets.

The following table shows the property values for a MailAddress object constructed using the preceding example address.

Property Value
DisplayName "Tom Smith"
Host "contoso.com"
User "tsmith"
Address "tsmith@contoso.com"

Applies to

MailAddress(String, String)

Source:
MailAddress.cs
Source:
MailAddress.cs
Source:
MailAddress.cs

Initializes a new instance of the MailAddress class using the specified address and display name.

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)

Parameters

address
String

A String that contains an email address.

displayName
String

A String that contains the display name associated with address. This parameter can be null.

Exceptions

address is null.

address is Empty ("").

address is not in a recognized format.

-or-

address contains non-ASCII characters.

Examples

The following code example uses this constructor to create MailAddress instances for the sender and recipient of an email message.

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());
    }
}

Remarks

Leading and trailing white space in the display name is preserved.

If displayName contains non-ASCII characters, the iso-8859-1 character set is used for the displayName encoding. Encoding non-ASCII characters is discussed in RFC 1522, which is available at https://www.ietf.org/.

If address contains a display name, and displayName is not null and is not equal to String.Empty, displayName overrides the value specified in address.

Applies to

MailAddress(String, String, Encoding)

Source:
MailAddress.cs
Source:
MailAddress.cs
Source:
MailAddress.cs

Initializes a new instance of the MailAddress class using the specified address, display name, and encoding.

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)

Parameters

address
String

A String that contains an email address.

displayName
String

A String that contains the display name associated with address.

displayNameEncoding
Encoding

The Encoding that defines the character set used for displayName.

Exceptions

address is null.

-or-

displayName is null.

address is Empty ("").

-or-

displayName is Empty ("").

address is not in a recognized format.

-or-

address contains non-ASCII characters.

Examples

The following code example uses this constructor to create MailAddress instances for the sender of an email message.

// 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)

Remarks

Leading and trailing white space in the display name is preserved.

If address contains a display name, and displayName is not null and is not equal to String.Empty, displayName overrides the value specified in address.

The MailAddress method does not check if the displayName parameter is valid. This method removes surrounding quotes not displayed by the DisplayName property. Quotes will be added before transmission, except where ASCII or Unicode is specified in the displayNameEncoding parameter. The encoding specified in the displayNameEncoding parameter will be applied to the DisplayName property before transmission ASCII or Unicode is specified in the displayNameEncoding parameter. UTF8 is the default encoding if none is specified.

For more information on supported mail address formats, see MailAddress.

Applies to