2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System / System.Net.Mail / MailAddress.cs
bloba5d9fc68b3c7ce022091c85191fdd39d6c53fa99
1 //
2 // System.Net.Mail.MailAddress.cs
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
31 #if NET_2_0
33 using System.Text;
35 namespace System.Net.Mail {
36 public class MailAddress
38 #region Fields
40 string address;
41 string displayName;
42 //Encoding displayNameEncoding;
44 #endregion // Fields
46 #region Constructors
48 public MailAddress (string address) : this (address, null)
52 public MailAddress (string address, string displayName) : this (address, displayName, Encoding.Default)
56 public MailAddress (string address, string displayName, Encoding displayNameEncoding)
58 if (address == null)
59 throw new ArgumentNullException ("address");
61 // either displayname is enclosed in quotes, and/or e-mail address
62 // is enclosed in less than / greater than characters
63 int quoteStart = address.IndexOf ('"');
64 if (quoteStart == 0) {
65 int quoteEnd = address.IndexOf ('"', quoteStart + 1);
66 if (quoteEnd == -1)
67 throw CreateFormatException ();
68 this.displayName = address.Substring (quoteStart + 1, quoteEnd - 1).Trim ();
69 address = address.Substring (quoteEnd + 1);
72 int addressStart = address.IndexOf ('<');
73 if (addressStart != -1) {
74 if (addressStart + 1 >= address.Length)
75 throw CreateFormatException ();
76 int addressEnd = address.IndexOf ('>', addressStart + 1);
77 if (addressEnd == -1)
78 throw CreateFormatException ();
79 if (this.displayName == null)
80 this.displayName = address.Substring (0, addressStart).Trim ();
81 address = address.Substring (++addressStart, addressEnd - addressStart);
84 // LAMESPEC: zero-length displayName should not override display name
85 // specified in address
86 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=283163
87 if (displayName != null)
88 this.displayName = displayName.Trim ();
90 this.address = address.Trim ();
91 //this.displayNameEncoding = displayNameEncoding;
94 #endregion // Constructors
96 #region Properties
98 public string Address {
99 get { return address; }
102 public string DisplayName {
103 get {
104 if (displayName == null)
105 return string.Empty;
106 return displayName;
110 public string Host {
111 get { return Address.Substring (address.IndexOf ("@") + 1); }
114 public string User {
115 get { return Address.Substring (0, address.IndexOf ("@")); }
118 #endregion // Properties
120 #region Methods
122 public override bool Equals (object obj)
124 return Equals (obj as MailAddress);
127 bool Equals (MailAddress other)
129 return other != null && Address == other.Address;
132 public override int GetHashCode ()
134 return address.GetHashCode ();
137 public override string ToString ()
139 StringBuilder sb = new StringBuilder ();
140 if (DisplayName != null && DisplayName.Length > 0) {
141 sb.Append ("\"");
142 sb.Append (DisplayName);
143 sb.Append ("\"");
144 sb.Append (" ");
145 sb.Append ("<");
146 sb.Append (Address);
147 sb.Append (">");
149 else {
150 sb.Append (Address);
153 return sb.ToString ();
156 private static FormatException CreateFormatException () {
157 return new FormatException ("The specified string is not in the "
158 + "form required for an e-mail address.");
161 #endregion // Methods
165 #endif // NET_2_0