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.
Count { get; }Contains count of mail addresses.
DisplayName { get; set; }Gets or sets a display name.
Host { get; }Gets the host portion of the address.
Id { get; }Gets object identification information
Item { get; }Gets the element at the specified index.
OriginalAddressString { get; }Gets or sets the original e-mail address string.
ParticipationStatus { get; set; }Gets or sets the participation status for the calendar user.
User { get; }Gets the username.
X500Address { get; }Gets the email address in Exchange format, if it exists.

Methods

NameDescription
override Equals(object)Determines whether the specified Object is equal to this instance.
override GetHashCode()Returns a hash code for this instance.
override ToString()Returns a String that represents this instance.
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