Class MailAddress

MailAddress class

Represents an email address with support for display names, encoding, and address parsing. This class provides functionality for working with email addresses in email messages, including properties for the display name, user name, host, and raw address. It supports proper encoding of international characters in display names and validates email addresses against SMTP standards.

public class MailAddress : IMailAddress

Constructors

NameDescription
MailAddress(string)Initializes a new instance of the MailAddress class.
MailAddress(string, bool)Initializes a new instance of the MailAddress class.
MailAddress(string, string)Initializes a new instance of the MailAddress class.
MailAddress(string, string, bool)Initializes a new instance of the MailAddress class.
MailAddress(string, string, Encoding)Initializes a new instance of the MailAddress class.
MailAddress(string, string, Encoding, bool)Initializes a new instance of the MailAddress class.

Properties

NameDescription
Address { get; set; }Gets or sets the e-mail address, such as john@example.com.
Count { get; }Gets the number of addresses, which is always 1. Together with the indexer it lets an address be used where a list is expected.
DisplayName { get; set; }Gets or sets the display name, such as John Smith.
Host { get; }Gets the host part of the address: the part after @.
Id { get; }Gets the identifiers of the address on the server it came from, such as Exchange.
Item { get; }Gets the address itself; the index is ignored.
OriginalAddressString { get; }Gets the address exactly as it appeared in the message, before it was parsed.
ParticipationStatus { get; set; }Gets or sets the response of the attendee to a meeting request, when the address is a meeting attendee.
User { get; }Gets the user part of the address: the part before @.
X500Address { get; }Gets the Exchange (X.500) form of the address, if the message has one.

Methods

NameDescription
override Equals(object)Determines whether the specified object is the same e-mail address.
override GetHashCode()Returns a hash code for this instance.
override ToString()Returns the address with its display name, as written in a header.
implicit operatorPerforms an implicit conversion from String to MailAddress. (2 operators)

Remarks

The MailAddress class can represent both simple email addresses (e.g., “user@example.com”) and addresses with display names (e.g., “John Elias <john@example.com>”). It automatically handles encoding of non-ASCII characters in display names and validates addresses to ensure they comply with SMTP standards.

Examples

The following example shows how to create mail addresses with and without display names.

[C#]

// Create a simple email address
var address1 = new MailAddress("user@example.com");

// Create an address with display name
var address2 = new MailAddress("john@example.com", "John Doe");

// Create an address with encoded display name (international characters)
var encoding = Encoding.UTF8;
var address3 = new MailAddress("user@example.com", "Андрей Иванов", encoding);

// Access address properties
Console.WriteLine("Address: " + address1.Address);
Console.WriteLine("Display Name: " + address2.DisplayName);
Console.WriteLine("User: " + address2.User);
Console.WriteLine("Host: " + address2.Host);

[Visual Basic]

' Create a simple email address
Dim address1 As New MailAddress("user@example.com")

' Create an address with display name
Dim address2 As New MailAddress("john@example.com", "John Doe")

' Create an address with encoded display name (international characters)
Dim encoding As Encoding = Encoding.UTF8
Dim address3 As New MailAddress("user@example.com", "Андрей Иванов", encoding)

' Access address properties
Console.WriteLine("Address: " + address1.Address)
Console.WriteLine("Display Name: " + address2.DisplayName)
Console.WriteLine("User: " + address2.User)
Console.WriteLine("Host: " + address2.Host)

See Also