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.
35 namespace System.Net.Mail {
36 public class MailAddress
45 //Encoding displayNameEncoding;
51 public MailAddress (string address) : this (address, null)
55 public MailAddress (string address, string displayName) : this (address, displayName, Encoding.UTF8)
59 [MonoTODO ("We don't do anything with displayNameEncoding")]
60 public MailAddress (string address, string displayName, Encoding displayNameEncoding)
63 throw new ArgumentNullException ("address");
64 if (address.Length == 0)
65 throw new ArgumentException ("address");
67 if (displayName != null)
68 this.displayName = displayName.Trim ();
69 ParseAddress (address);
72 void ParseAddress (string address)
74 // 1. Quotes for display name
75 address = address.Trim ();
76 int idx = address.IndexOf ('"');
78 if (idx != 0 || address.Length == 1)
79 throw CreateFormatException ();
81 int closing = address.LastIndexOf ('"');
83 throw CreateFormatException ();
85 if (this.displayName == null)
86 this.displayName = address.Substring (idx + 1, closing - idx - 1).Trim ();
87 address = address.Substring (closing + 1).Trim ();
91 idx = address.IndexOf ('<');
93 if (this.displayName == null)
94 this.displayName = address.Substring (0, idx).Trim ();
95 if (address.Length - 1 == idx)
96 throw CreateFormatException ();
98 int end = address.IndexOf ('>', idx + 1);
100 throw CreateFormatException ();
102 address = address.Substring (idx + 1, end - idx - 1).Trim ();
104 this.address = address;
106 idx = address.IndexOf ('@');
108 throw CreateFormatException ();
109 if (idx != address.LastIndexOf ('@'))
110 throw CreateFormatException ();
112 this.user = address.Substring (0, idx).Trim ();
113 if (user.Length == 0)
114 throw CreateFormatException ();
115 this.host = address.Substring (idx + 1).Trim ();
116 if (host.Length == 0)
117 throw CreateFormatException ();
120 #endregion // Constructors
124 public string Address {
125 get { return address; }
128 public string DisplayName {
130 if (displayName == null)
144 #endregion // Properties
148 public override bool Equals (object obj)
153 return (0 == String.Compare (ToString (), obj.ToString (), StringComparison.OrdinalIgnoreCase));
156 public override int GetHashCode ()
158 return ToString ().GetHashCode ();
161 public override string ToString ()
163 if (to_string != null)
166 if (!String.IsNullOrEmpty (displayName))
167 to_string = String.Format ("\"{0}\" <{1}>", DisplayName, Address);
174 private static FormatException CreateFormatException () {
175 return new FormatException ("The specified string is not in the "
176 + "form required for an e-mail address.");
179 #endregion // Methods