2 // System.Net.Mail.MailAddress.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2004
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 namespace System.Net.Mail {
34 public class MailAddress
43 //Encoding displayNameEncoding;
49 public MailAddress (string address) : this (address, null)
53 public MailAddress (string address, string displayName) : this (address, displayName, Encoding.UTF8)
57 [MonoTODO ("We don't do anything with displayNameEncoding")]
58 public MailAddress (string address, string displayName, Encoding displayNameEncoding)
61 throw new ArgumentNullException ("address");
62 if (address.Length == 0)
63 throw new ArgumentException ("address");
65 if (displayName != null)
66 this.displayName = displayName.Trim ();
67 ParseAddress (address);
70 void ParseAddress (string address)
72 // 1. Quotes for display name
73 address = address.Trim ();
74 int idx = address.IndexOf ('"');
76 if (idx != 0 || address.Length == 1)
77 throw CreateFormatException ();
79 int closing = address.LastIndexOf ('"');
81 throw CreateFormatException ();
83 if (this.displayName == null)
84 this.displayName = address.Substring (idx + 1, closing - idx - 1).Trim ();
85 address = address.Substring (closing + 1).Trim ();
89 idx = address.IndexOf ('<');
91 if (this.displayName == null)
92 this.displayName = address.Substring (0, idx).Trim ();
93 if (address.Length - 1 == idx)
94 throw CreateFormatException ();
96 int end = address.IndexOf ('>', idx + 1);
98 throw CreateFormatException ();
100 address = address.Substring (idx + 1, end - idx - 1).Trim ();
102 this.address = address;
104 idx = address.IndexOf ('@');
106 throw CreateFormatException ();
107 if (idx != address.LastIndexOf ('@'))
108 throw CreateFormatException ();
110 this.user = address.Substring (0, idx).Trim ();
111 if (user.Length == 0)
112 throw CreateFormatException ();
113 this.host = address.Substring (idx + 1).Trim ();
114 if (host.Length == 0)
115 throw CreateFormatException ();
118 #endregion // Constructors
122 public string Address {
123 get { return address; }
126 public string DisplayName {
128 if (displayName == null)
142 #endregion // Properties
146 public override bool Equals (object obj)
151 return (0 == String.Compare (ToString (), obj.ToString (), StringComparison.OrdinalIgnoreCase));
154 public override int GetHashCode ()
156 return ToString ().GetHashCode ();
159 public override string ToString ()
161 if (to_string != null)
164 if (!String.IsNullOrEmpty (displayName))
165 to_string = String.Format ("\"{0}\" <{1}>", DisplayName, Address);
172 private static FormatException CreateFormatException () {
173 return new FormatException ("The specified string is not in the "
174 + "form required for an e-mail address.");
177 #endregion // Methods