1 out of 5 rated this helpful - Rate this topic

MailAddress Class

Represents the address of an electronic mail sender or recipient.

System.Object
  System.Net.Mail.MailAddress

Namespace:  System.Net.Mail
Assembly:  System (in System.dll)
public class MailAddress

The MailAddress type exposes the following members.

  Name Description
Public method MailAddress(String) Initializes a new instance of the MailAddress class using the specified address.
Public method MailAddress(String, String) Initializes a new instance of the MailAddress class using the specified address and display name.
Public method MailAddress(String, String, Encoding) Initializes a new instance of the MailAddress class using the specified address, display name, and encoding.
Top
  Name Description
Public property Address Gets the e-mail address specified when this instance was created.
Public property DisplayName Gets the display name composed from the display name and address information specified when this instance was created.
Public property Host Gets the host portion of the address specified when this instance was created.
Public property User Gets the user information from the address specified when this instance was created.
Top
  Name Description
Public method Equals Compares two mail addresses. (Overrides Object.Equals(Object).)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Returns a hash value for a mail address. (Overrides Object.GetHashCode().)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string representation of this instance. (Overrides Object.ToString().)
Top

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 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, Host name for "display name" user@host if these are not included.

  • Quotes are added around the DisplayName for display name <user@host>, if these are not included.

  • Unicode characters are supported in the DisplayName. property.

  • 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 note quoted, all text between the start of the string (or comma) and the address are considered part of the DisplayName, including comments.

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

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Sample code - in PowerShell
<#
.SYNOPSIS
This script creates and sends an SMTP Mail Mesage
.DESCRIPTION
This script creates a mail message, then sends it. This is
a re-write of an MSDN sample.
.NOTES
File Name : Send-SMTPMessage2.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx
.EXAMPLE
PSH [C:\foo]: .\Send-SMTPMessage2.ps1'
Sending an e-mail message to The Doctor and tfl@powershell.com
#>
##
# Start of Script
##
# Create To/From Addresses using System.Net.Mail.MailAddress
$from = New-Object System.Net.Mail.Mailaddress "tfl@psp.co.uk","Thomas Lee"
$to = New-Object System.Net.Mail.Mailaddress "doctordns@gmail.com","The Doctor"
# Create the mail message and add subject and body
$message = New-Object System.Net.Mail.MailMessage $from,$to
$message.Subject = "Using the SmtpClient class."
$message.Body = "Using this feature, you can send an e-mail message from "
$message.Body += "any application very easily."
# Create and Add a BCC
$bcc = New-Object System.Net.Mail.MailAddress "tfl@powershell.com"
$message.Bcc.Add($bcc)
# Create SMTP Client
$Server="Cookham8"
$client = New-Object System.Net.Mail.SmtpClient($server);
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
# Send SMTP Mail
"Sending an e-mail message to {0} and {1}" -f $to.DisplayName, $message.Bcc.ToString()
try {
$client.Send($message)
}
catch {
"Exception caught in sending mail"
$Error[0]
}
# End of Script