2
0

MailAddress.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // System.Web.Mail.MailAddress.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. using System;
  9. namespace System.Web.Mail {
  10. // Reperesents a mail address
  11. internal class MailAddress {
  12. protected string user;
  13. protected string host;
  14. protected string name;
  15. public string User {
  16. get { return user; }
  17. set { user = value; }
  18. }
  19. public string Host {
  20. get { return host; }
  21. set { host = value; }
  22. }
  23. public string Name {
  24. get { return name; }
  25. set { name = value; }
  26. }
  27. public string Address {
  28. get { return String.Format( "{0}@{1}" , user , host ); }
  29. set {
  30. string[] parts = value.Split( new char[] { '@' } );
  31. if( parts.Length != 2 )
  32. throw new FormatException( "Email address is incorrect: " + value );
  33. user = parts[ 0 ];
  34. host = parts[ 1 ];
  35. }
  36. }
  37. public static MailAddress Parse( string str ) {
  38. MailAddress addr = new MailAddress();
  39. string address = null;
  40. string nameString = null;
  41. string[] parts = str.Split( new char[] { ' ' } );
  42. // find the address: [email protected]
  43. // and put to gether all the parts
  44. // before the address as nameString
  45. foreach( string part in parts ) {
  46. if( part.IndexOf( '@' ) > 0 ) {
  47. address = part;
  48. break;
  49. }
  50. nameString = nameString + part + " ";
  51. }
  52. if( address == null )
  53. throw new FormatException( "Email address not found in: " + str );
  54. address = address.Trim( new char[] { '<' , '>' , '(' , ')' } );
  55. addr.Address = address;
  56. if( nameString != null ) {
  57. addr.Name = nameString.Trim();
  58. addr.Name = ( addr.Name.Length == 0 ? null : addr.Name );
  59. }
  60. return addr;
  61. }
  62. public override string ToString() {
  63. string retString = "";
  64. if( name == null ) {
  65. retString = String.Format( "<{0}>" , this.Address );
  66. } else {
  67. retString = String.Format( "\"{0}\" <{1}>" , this.Name , this.Address);
  68. }
  69. return retString;
  70. }
  71. }
  72. }