MailAddress.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.Web.Mail.MailAddress.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Text;
  30. namespace System.Web.Mail {
  31. // Reperesents a mail address
  32. internal class MailAddress {
  33. protected string user;
  34. protected string host;
  35. protected string name;
  36. public string User {
  37. get { return user; }
  38. set { user = value; }
  39. }
  40. public string Host {
  41. get { return host; }
  42. set { host = value; }
  43. }
  44. public string Name {
  45. get { return name; }
  46. set { name = value; }
  47. }
  48. public string Address {
  49. get { return String.Format( "{0}@{1}" , user , host ); }
  50. set {
  51. string[] parts = value.Split( new char[] { '@' } );
  52. if( parts.Length != 2 )
  53. throw new FormatException( "Invalid e-mail address: '" + value + "'.");
  54. user = parts[ 0 ];
  55. host = parts[ 1 ];
  56. }
  57. }
  58. public static MailAddress Parse( string str ) {
  59. if (str == null || str.Trim () == "")
  60. return null;
  61. MailAddress addr = new MailAddress();
  62. string address = null;
  63. string nameString = null;
  64. string[] parts = str.Split( new char[] { ' ' } );
  65. // find the address: [email protected]
  66. // and put to gether all the parts
  67. // before the address as nameString
  68. foreach( string part in parts ) {
  69. if( part.IndexOf( '@' ) > 0 ) {
  70. address = part;
  71. break;
  72. }
  73. nameString = nameString + part + " ";
  74. }
  75. if( address == null )
  76. throw new FormatException( "Invalid e-mail address: '" + str + "'.");
  77. address = address.Trim( new char[] { '<' , '>' , '(' , ')' } );
  78. addr.Address = address;
  79. if( nameString != null ) {
  80. addr.Name = nameString.Trim( new char[] { ' ' , '"' } );
  81. addr.Name = ( addr.Name.Length == 0 ? null : addr.Name );
  82. }
  83. return addr;
  84. }
  85. public override string ToString() {
  86. string retString = "";
  87. if( name == null ) {
  88. retString = String.Format( "<{0}>" , this.Address );
  89. } else {
  90. string personName = this.Name;
  91. if( MailUtil.NeedEncoding( personName ) ) {
  92. personName = String.Format( "=?{0}?B?{1}?=",
  93. Encoding.Default.BodyName ,
  94. MailUtil.Base64Encode( personName ) ) ;
  95. }
  96. retString = String.Format( "\"{0}\" <{1}>" , personName , this.Address);
  97. }
  98. return retString;
  99. }
  100. }
  101. }