MailAddress.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // System.Net.Mail.MailAddress.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2004
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Text;
  30. namespace System.Net.Mail {
  31. public class MailAddress
  32. {
  33. #region Fields
  34. string address;
  35. string displayName;
  36. string host;
  37. string user;
  38. string to_string;
  39. //Encoding displayNameEncoding;
  40. #endregion // Fields
  41. #region Constructors
  42. public MailAddress (string address) : this (address, null)
  43. {
  44. }
  45. public MailAddress (string address, string displayName) : this (address, displayName, Encoding.UTF8)
  46. {
  47. }
  48. [MonoTODO ("We don't do anything with displayNameEncoding")]
  49. public MailAddress (string address, string displayName, Encoding displayNameEncoding)
  50. {
  51. if (address == null)
  52. throw new ArgumentNullException ("address");
  53. if (address.Length == 0)
  54. throw new ArgumentException ("address");
  55. if (displayName != null)
  56. this.displayName = displayName.Trim ();
  57. ParseAddress (address);
  58. }
  59. void ParseAddress (string address)
  60. {
  61. // 1. Quotes for display name
  62. address = address.Trim ();
  63. int idx = address.IndexOf ('"');
  64. if (idx != -1) {
  65. if (idx != 0 || address.Length == 1)
  66. throw CreateFormatException ();
  67. int closing = address.LastIndexOf ('"');
  68. if (closing == idx)
  69. throw CreateFormatException ();
  70. if (this.displayName == null)
  71. this.displayName = address.Substring (idx + 1, closing - idx - 1).Trim ();
  72. address = address.Substring (closing + 1).Trim ();
  73. }
  74. // 2. <email>
  75. idx = address.IndexOf ('<');
  76. if (idx >= 0) {
  77. if (this.displayName == null)
  78. this.displayName = address.Substring (0, idx).Trim ();
  79. if (address.Length - 1 == idx)
  80. throw CreateFormatException ();
  81. int end = address.IndexOf ('>', idx + 1);
  82. if (end == -1)
  83. throw CreateFormatException ();
  84. address = address.Substring (idx + 1, end - idx - 1).Trim ();
  85. }
  86. this.address = address;
  87. // 3. email
  88. idx = address.IndexOf ('@');
  89. if (idx <= 0)
  90. throw CreateFormatException ();
  91. if (idx != address.LastIndexOf ('@'))
  92. throw CreateFormatException ();
  93. this.user = address.Substring (0, idx).Trim ();
  94. if (user.Length == 0)
  95. throw CreateFormatException ();
  96. this.host = address.Substring (idx + 1).Trim ();
  97. if (host.Length == 0)
  98. throw CreateFormatException ();
  99. }
  100. #endregion // Constructors
  101. #region Properties
  102. public string Address {
  103. get { return address; }
  104. }
  105. public string DisplayName {
  106. get {
  107. if (displayName == null)
  108. return string.Empty;
  109. return displayName;
  110. }
  111. }
  112. public string Host {
  113. get { return host; }
  114. }
  115. public string User {
  116. get { return user; }
  117. }
  118. #endregion // Properties
  119. #region Methods
  120. public override bool Equals (object obj)
  121. {
  122. if (obj == null)
  123. return false;
  124. return (0 == String.Compare (ToString (), obj.ToString (), StringComparison.OrdinalIgnoreCase));
  125. }
  126. public override int GetHashCode ()
  127. {
  128. return ToString ().GetHashCode ();
  129. }
  130. public override string ToString ()
  131. {
  132. if (to_string != null)
  133. return to_string;
  134. if (!String.IsNullOrEmpty (displayName))
  135. to_string = String.Format ("\"{0}\" <{1}>", DisplayName, Address);
  136. else
  137. to_string = address;
  138. return to_string;
  139. }
  140. private static FormatException CreateFormatException () {
  141. return new FormatException ("The specified string is not in the "
  142. + "form required for an e-mail address.");
  143. }
  144. #endregion // Methods
  145. }
  146. }