MailAddress Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents the address of an electronic mail sender or recipient.
public ref class MailAddress
public class MailAddress
type MailAddress = class
Public Class MailAddress
- Inheritance
-
MailAddress
The following code example demonstrates sending an email message by using the SmtpClient, MailAddress, and MailMessage classes.
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());
}
}
The MailAddress class is used by the SmtpClient and MailMessage classes to store address information for email messages.
A mail address is composed of a User name, Host name and optionally, a DisplayName. The DisplayName can contain non-ASCII characters if you encode them.
The MailAddress class supports the following mail address formats:
A simple address format of
user@host
. If a DisplayName is not set, this is the mail address format generated.A standard quoted display name format of
"display name" <user@host>
. If a DisplayName is set, this is the format generated.Angle brackets are added around the User name and Host name, if these are not included. For example,
"display name" user@host
is changed to"display name" <user@host>
.Quotes are added around the DisplayName, if these are not included. For example,
display name <user@host>
is changed to"display name" <user@host>
.Unicode characters are supported in the DisplayName property.
Within these two formats, the following elements are allowed:
A User name with quotes. For example,
"user name"@host
.Consecutive and trailing dots in user names. For example,
user...name..@host
.Bracketed domain literals. For example,
<user@[my domain]>
.Comments. For example,
(comment)"display name"(comment)<(comment)user(comment)@(comment)domain(comment)>(comment)
. Comments are removed before transmission.
A comma is used to separate elements in a list of mail addresses. As a result, a comma should not be used in unquoted display names in a list. The following mail addresses would be allowed:
"John, Doe" <user@host>, "Bob, Smith" <user2@host>
The following mail address would not be allowed:
John, Doe <user@host>, Bob, Smith <user2@host>
Quotes can be embedded in a quoted string, but they must be escaped. The following mail addresses would be allowed:
"John \"Jr\" Doe" <user@host>
"\"John \\\"Jr\\\" Doe\" <user@host>"
The following mail address would not be allowed:
"John "Jr" Doe" <user@host>
When the username is not quoted, all text between the start of the string (or comma) and the address are considered part of the DisplayName, including comments. For example:
(non comment) unquoted display (non comment) name (non comment) <user@host>
Although the MailAddress class accepts a mail address as valid, other mail servers may not accept the mail address.
The MailAddress class does not support the following mail address formats:
Mixed quoted and unquoted display names. For example,
display "name" <user@host>
.Groups, as defined in RFC 2822 Section 3.4 published by the IETF.
The obsolete user name formats of
"user"."name"@host
,user."name"@host
or"user".name@host
.
Mail |
Initializes a new instance of the MailAddress class using the specified address, display name, and encoding. |
Mail |
Initializes a new instance of the MailAddress class using the specified address and display name. |
Mail |
Initializes a new instance of the MailAddress class using the specified address. |
Address |
Gets the email address specified when this instance was created. |
Display |
Gets the display name composed from the display name and address information specified when this instance was created. |
Host |
Gets the host portion of the address specified when this instance was created. |
User |
Gets the user information from the address specified when this instance was created. |
Equals(Object) |
Compares two mail addresses. |
Get |
Returns a hash value for a mail address. |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
To |
Returns a string representation of this instance. |
Try |
Creates a new MailAddress. Does not throw an exception if the object cannot be created. |
Try |
Create a new MailAddress. Does not throw an exception if the object cannot be created. |
Try |
Create a new MailAddress. Does not throw an exception if the object cannot be created. |
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: