MailAddress Class
Represents the address of an electronic mail sender or recipient.
Assembly: System (in System.dll)
The MailAddress class is used by the SmtpClient and MailMessage classes to store address information for e-mail 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 following code example demonstrates sending an e-mail message by using the SmtpClient, MailAddress, and MailMessage classes.
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 e-mail 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 e-mail 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() ); } }
public:
static void CreateCopyMessage(String* server)
{
MailAddress* from = new MailAddress(S"ben@contoso.com", S"Ben Miller");
MailAddress* to = new MailAddress(S"jane@contoso.com", S"Jane Clayton");
MailMessage* message = new MailMessage(from, to);
// message.Subject = "Using the SmtpClient class.";
message->Subject = S"Using the SmtpClient class.";
message->Body = S"Using this feature, you can send an e-mail message from an application very easily.";
// Add a carbon copy recipient.
MailAddress* copy= new MailAddress(S"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(S"Sending an e-mail message to {0} by using the SMTP host {1}.",
to->Address, client->Host);
client->Send(message);
client->Dispose();
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Is there a work around for this ?
- 1/28/2010
- Shobin Mathew
Display Name <Email Address>
So this would work:
MailAddress mail = new MailAddress("Matthew Harris <admin@example.com>");Why would anyone care about this undocumented syntax when there is a dyadic constructor available? Well it lets you pass this information down from places such as the MailDefinition from attribute where you only have a single string parameter to pass in!
- 7/19/2008
- MMCompton
- 7/1/2009
- Thomas Lee
The MailAddress class gives the Format Exception "The specified string is not in the form required for an e-mail address.".
Alternative is to use the System.Web.Mail class, but this class does not support alternate views.
- 9/4/2008
- apk_nl
- 7/1/2009
- Thomas Lee
Reviewing the following mail header and try to reconstruct original mail or reply to such message:
From: <Service-MArchiv-F>
To: <Service-MArchiv-G>
Subject: =?iso-8859-1?Q?=28outlookFiller_Trial_Version_Import=29_Adress=E4nderung?=
Date: Mon, 5 Jan 2009 17:38:25 +0200
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0009_01C9D3A3.F9522510"
X-Priority: 3
X-MSMail-Priority: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
This is a multi-part message in MIME format.
We catch FormatException: "The specified string is not in the form required for an e-mail address."
Now, it's not possible to send mails some symbolical named internal adresses.
Please correct this or provide any fix!
Thanks in advice.
- 5/13/2009
- _Alex_74_
- 7/1/2009
- Thomas Lee
- 6/24/2009
- Jeff_Tucker_NCL