Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
MailAddress Class
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
MailAddress Class

Updated: November 2007

Represents the address of an electronic mail sender or recipient.

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

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

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.

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);
            client.Send(message);
        }

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



J#
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.set_Subject("Using the SmtpClient class.");
    message.set_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.get_CC().Add(copy);
    SmtpClient client = new SmtpClient(server);
    // Include credentials if the server requires them.
    client.set_Credentials(CredentialCache.get_DefaultNetworkCredentials());
    Console.WriteLine("Sending an e-mail message to {0} by using the " 
        + "SMTP host {1}.", to.get_Address(), client.get_Host());
    client.Send(message);
} //CreateCopyMessage

System..::.Object
  System.Net.Mail..::.MailAddress
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
.Net.mail.mailaddress error "The specified string is not in the form..      SLareau   |   Edit   |  
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 What's this?: Add a tag
Flag as ContentBug
MailAddress doesn't support "quoted user"@mydomain.com      MMCompton ... Thomas Lee   |   Edit   |  
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?
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker