// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-#if NET_2_0
-
using System.Text;
namespace System.Net.Mail {
string address;
string displayName;
- Encoding displayNameEncoding;
+ string host;
+ string user;
+ string to_string;
+ //Encoding displayNameEncoding;
#endregion // Fields
#region Constructors
- public MailAddress (string address)
+ public MailAddress (string address) : this (address, null)
{
- this.address = address;
}
- public MailAddress (string address, string displayName)
+ public MailAddress (string address, string displayName) : this (address, displayName, Encoding.UTF8)
{
- this.address = address;
- this.displayName = displayName;
}
- public MailAddress (string address, string name, Encoding displayNameEncoding)
+ [MonoTODO ("We don't do anything with displayNameEncoding")]
+ public MailAddress (string address, string displayName, Encoding displayNameEncoding)
+ {
+ if (address == null)
+ throw new ArgumentNullException ("address");
+ if (address.Length == 0)
+ throw new ArgumentException ("address");
+
+ if (displayName != null)
+ this.displayName = displayName.Trim ();
+ ParseAddress (address);
+ }
+
+ 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;
- this.displayName = displayName;
- this.displayNameEncoding = displayNameEncoding;
+ // 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 ();
}
#endregion // Constructors
- #region Properties
+#region Properties
- public string Address {
+ public string Address {
get { return address; }
}
public string DisplayName {
- get { return displayName; }
+ get {
+ if (displayName == null)
+ return string.Empty;
+ return displayName;
+ }
}
public string Host {
- get { return Address.Substring (address.IndexOf ("@") + 1); }
+ get { return host; }
}
public string User {
- get { return Address.Substring (0, address.IndexOf ("@")); }
+ get { return user; }
}
- #endregion // Properties
+#endregion // Properties
+
+#region Methods
+
+ public override bool Equals (object obj)
+ {
+ if (obj == null)
+ return false;
- #region Methods
+ return (0 == String.Compare (ToString (), obj.ToString (), StringComparison.OrdinalIgnoreCase));
+ }
+
+ public override int GetHashCode ()
+ {
+ return ToString ().GetHashCode ();
+ }
public override string ToString ()
{
- StringBuilder sb = new StringBuilder ();
- if (DisplayName != null && DisplayName.Length > 0) {
- sb.Append (DisplayName);
- sb.Append (" ");
- }
- sb.Append ("<");
- sb.Append (Address);
- sb.Append (">");
+ if (to_string != null)
+ return to_string;
+
+ if (!String.IsNullOrEmpty (displayName))
+ to_string = String.Format ("\"{0}\" <{1}>", DisplayName, Address);
+ else
+ to_string = address;
+
+ return to_string;
+ }
- return sb.ToString ();
+ private static FormatException CreateFormatException () {
+ return new FormatException ("The specified string is not in the "
+ + "form required for an e-mail address.");
}
- #endregion // Methods
+#endregion // Methods
}
}
-#endif // NET_2_0