| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- //
- // System.Net.Mail.MailAddress.cs
- //
- // Author:
- // Tim Coleman ([email protected])
- //
- // Copyright (C) Tim Coleman, 2004
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.Text;
- namespace System.Net.Mail {
- public class MailAddress
- {
- #region Fields
- string address;
- string displayName;
- string host;
- string user;
- string to_string;
- //Encoding displayNameEncoding;
- #endregion // Fields
- #region Constructors
- public MailAddress (string address) : this (address, null)
- {
- }
- public MailAddress (string address, string displayName) : this (address, displayName, Encoding.UTF8)
- {
- }
- [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;
- // 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
- public string Address {
- get { return address; }
- }
- public string DisplayName {
- get {
- if (displayName == null)
- return string.Empty;
- return displayName;
- }
- }
- public string Host {
- get { return host; }
- }
- public string User {
- get { return user; }
- }
- #endregion // Properties
- #region Methods
-
- public override bool Equals (object obj)
- {
- if (obj == null)
- return false;
- return (0 == String.Compare (ToString (), obj.ToString (), StringComparison.OrdinalIgnoreCase));
- }
- public override int GetHashCode ()
- {
- return ToString ().GetHashCode ();
- }
- public override string ToString ()
- {
- 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;
- }
- private static FormatException CreateFormatException () {
- return new FormatException ("The specified string is not in the "
- + "form required for an e-mail address.");
- }
- #endregion // Methods
- }
- }
|