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
42 //Encoding displayNameEncoding;
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
)
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);
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);
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
98 public string Address
{
99 get { return address; }
102 public string DisplayName
{
104 if (displayName
== null)
111 get { return Address.Substring (address.IndexOf ("@") + 1); }
115 get { return Address.Substring (0, address.IndexOf ("@")); }
118 #endregion // Properties
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) {
142 sb
.Append (DisplayName
);
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