Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
MailAddress Class

  Switch on low bandwidth view
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

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



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

.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   |   Show History
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   |   Show History
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?
MailAddress does not support German Umlaut      apk_nl ... Thomas Lee   |   Edit   |   Show History
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.

Flag as ContentBug
MailAddress doesn't support symbolical mail names      _Alex_74_ ... Thomas Lee   |   Edit   |   Show History

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.

internationalization and MailAddress      Jeff_Tucker_NCL   |   Edit   |   Show History
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.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker