//
// This constructor validates and stores the components of an e-mail address.
//
// Preconditions:
// - 'address' must not be null or empty.
//
// Postconditions:
// - The e-mail address components from the given 'address' are parsed, which should be formatted as:
// "EncodedDisplayname" <username@host>
// - If a 'displayName' is provided separately, it overrides whatever display name is parsed from the 'address'
// field. The display name does not need to be pre-encoded if a 'displayNameEncoding' is provided.
//
// A FormatException will be thrown if any of the components in 'address' are invalid.
public MailAddress(string address, string displayName, Encoding displayNameEncoding)
{
if (address == null)
{
throw new ArgumentNullException("address");
}
if (address == String.Empty)
{
throw new ArgumentException(SR.GetString(SR.net_emptystringcall, "address"), "address");
}
this.displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding(MimeBasePart.defaultCharSet);
this.displayName = displayName ?? string.Empty;
// Check for bounding quotes
if (!String.IsNullOrEmpty(this.displayName))
{
this.displayName = MailAddressParser.NormalizeOrThrow(this.displayName);
if (this.displayName.Length >= 2 && this.displayName[0] == '\"' &&
this.displayName[this.displayName.Length - 1] == '\"')
{
// Peal bounding quotes, they'll get re-added later.
this.displayName = this.displayName.Substring(1, this.displayName.Length - 2);
}
}
MailAddress result = MailAddressParser.ParseAddress(address);
this.host = result.host;
this.userName = result.userName;
// If we were not given a display name, use the one parsed from 'address'.
if (String.IsNullOrEmpty(this.displayName))
{
this.displayName = result.displayName;
}
}