.NET Framework Class Library
MailAddress Class

Represents the address of an electronic mail sender or recipient.

Namespace:  System.Net.Mail
Assembly:  System (in System.dll)
Syntax

Visual Basic (Declaration)
Public Class MailAddress
Visual Basic (Usage)
Dim instance As MailAddress
C#
public class MailAddress
Visual C++
public ref class MailAddress
JScript
public class MailAddress
Remarks

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.

Examples

The following code example demonstrates sending an e-mail message by using the SmtpClient, MailAddress, and MailMessage classes.

C#
        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() );
        }
    }
Visual C++
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 e-mail 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 e-mail message to {0} by using the SMTP host {1}.", to->Address, client->Host );
   client->Send( message );
   client->~SmtpClient();
}


Inheritance Hierarchy

System..::.Object
  System.Net.Mail..::.MailAddress
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

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.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Community Content

SLareau
.Net.mail.mailaddress error "The specified string is not in the form..
System.Net.mail.mailaddress throws an error "The specified string is not in the form required for an e-mail address." if the username portion of the email address contains agave mark such as mè@somewhere.com . Is this an oversight? If not what is the recommended method to handel this so a email can be sent to the above address? RFC 2822 seems to allow any [Alphabetic] character.
Tags :

Thomas Lee
MailAddress doesn't support "quoted user"@mydomain.com
System.Net.Mail.MailAddress also throws the "not in the form" error when the first part of the email address is surrounded in double-quotes. This syntax is allowed by RFC 2822, why is it not supported by the MailAddress class?

Thomas Lee
MailAddress does not support German Umlaut
MailAddress does not support German Umlaut (e.g. hölm@hotmail.com), but this e-mail address is a valid e-mail address.

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.

Tags : feature

Thomas Lee
MailAddress doesn't support symbolical mail names

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.


Jeff_Tucker_NCL
internationalization and MailAddress
Thomas Lee is correct in that a quoted local part is valid in an email address and certain mail addresses may be invalid if not in a quoted string. However, the characters that others of you have mentioned such as the umlaut and the agave are not in the ASCII character set, they are extended ASCII. In RFC 2822 (and subsequent RFC's 5322 and 3696) the dtext specification (allowed in quoted local parts) only allows most ASCII values (RFC 2822, section 3.4.1) which includes values in ranges from 33-90 and 94-126. RFC 5335 has been proposed that would allow non-ascii characters in the addr-spec, however it is still labeled as experimental and as such is not supported in MailAddress.

rtpHarry
How to pass the Display Name and Email in a single parameter constructor.
When constructing a new MailAddress instance you can pass an email address and a display name in as a single parameter as long as it follows the form:

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!


Page view tracker