void ParseAddress(string address)
{
// 1. Quotes for display name
address = address.Trim();
int idx = address.IndexOf('"');
if (idx != -1)
{
if (idx != 0 || address.Length == 1)
throw CreateFormatException();
int closing = address.LastIndexOf('"');
if (closing == idx)
throw CreateFormatException();
if (this.DisplayName == null)
this.DisplayName = address.Substring(idx + 1, closing - idx - 1).Trim();
address = address.Substring(closing + 1).Trim();
}
// 2. <email>
idx = address.IndexOf('<');
if (idx >= 0)
{
if (this.DisplayName == null)
this.DisplayName = address.Substring(0, idx).Trim();
if (address.Length - 1 == idx)
throw CreateFormatException();
int end = address.IndexOf('>', idx + 1);
if (end == -1)
throw CreateFormatException();
address = address.Substring(idx + 1, end - idx - 1).Trim();
}
this.Address = address;
// 3. email
idx = address.IndexOf('@');
if (idx <= 0)
throw CreateFormatException();
if (idx != address.LastIndexOf('@'))
throw CreateFormatException();
this.User = address.Substring(0, idx).Trim();
if (User.Length == 0)
throw CreateFormatException();
this.Host = address.Substring(idx + 1).Trim();
if (Host.Length == 0)
throw CreateFormatException();
}