PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/HigLaboNet/HigLabo.Mail/Common/MailAddress.cs

#
C# | 298 lines | 200 code | 4 blank | 94 comment | 31 complexity | b24715b60587c88f462d5945e09b5b43 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Globalization;
  6. using HigLabo.Net.Smtp;
  7. namespace HigLabo.Net.Mail
  8. {
  9. /// Represent mailaddress when sending by smtp.
  10. /// <summary>
  11. /// Represent mailaddress when sending by smtp.
  12. /// </summary>
  13. public class MailAddress
  14. {
  15. private class RegexList
  16. {
  17. public static Regex DisplayName_MailAddress = new Regex("(?<DisplayName>.*)<(?<MailAddress>[^>]*)>");
  18. public static Regex MailAddressWithBracket = new Regex("<(?<MailAddress>[^>]*)>");
  19. }
  20. private String _Value = "";
  21. private String _DisplayName = "";
  22. private String _UserName = "";
  23. private String _DomainName = "";
  24. private Boolean _IsDoubleQuote = false;
  25. private Encoding _Encoding = Encoding.ASCII;
  26. private TransferEncoding _TransferEncoding = TransferEncoding.Base64;
  27. /// ?????????????????????
  28. /// <summary>
  29. /// Get or set mailaddress value.
  30. /// ?????????????????????
  31. /// </summary>
  32. public String Value
  33. {
  34. get { return this._Value; }
  35. set { this._Value = value; }
  36. }
  37. /// ???????????????
  38. /// <summary>
  39. /// ???????????????
  40. /// </summary>
  41. public String DisplayName
  42. {
  43. get { return this._DisplayName; }
  44. set { this._DisplayName = value; }
  45. }
  46. /// ?????????????????
  47. /// <summary>
  48. /// ?????????????????
  49. /// </summary>
  50. public String UserName
  51. {
  52. get { return this._UserName; }
  53. set { this._UserName = value; }
  54. }
  55. /// ?????????????????
  56. /// <summary>
  57. /// ?????????????????
  58. /// </summary>
  59. public String DomainName
  60. {
  61. get { return this._DomainName; }
  62. set { this._DomainName = value; }
  63. }
  64. /// ????????????????????????????????????
  65. /// <summary>
  66. /// ????????????????????????????????????
  67. /// </summary>
  68. public Boolean IsDoubleQuote
  69. {
  70. get { return this._IsDoubleQuote; }
  71. set { this._IsDoubleQuote = value; }
  72. }
  73. /// ??????????????Encoding????????????
  74. /// <summary>
  75. /// ??????????????Encoding????????????
  76. /// </summary>
  77. public Encoding Encoding
  78. {
  79. get { return this._Encoding; }
  80. set { this._Encoding = value; }
  81. }
  82. /// ??????????????TransferEncoding????????????
  83. /// <summary>
  84. /// ??????????????TransferEncoding????????????
  85. /// </summary>
  86. public TransferEncoding TransferEncoding
  87. {
  88. get { return this._TransferEncoding; }
  89. set { this._TransferEncoding = value; }
  90. }
  91. /// <summary>
  92. ///
  93. /// </summary>
  94. /// <param name="mailAddress"></param>
  95. public MailAddress(String mailAddress)
  96. {
  97. if (String.IsNullOrEmpty(mailAddress) == true)
  98. { throw new FormatException(); }
  99. if (mailAddress.Contains("@") == false)
  100. { throw new FormatException("Mail address must be contain @ char."); }
  101. Match m = RegexList.MailAddressWithBracket.Match(mailAddress);
  102. if (m.Success == true)
  103. {
  104. this._Value = m.Groups["MailAddress"].Value;
  105. }
  106. else
  107. {
  108. this._Value = mailAddress;
  109. }
  110. String[] ss = _Value.Split('@');
  111. this._UserName = ss[0];
  112. this._DomainName = ss[1];
  113. this.InitializeProperty();
  114. }
  115. /// <summary>
  116. ///
  117. /// </summary>
  118. /// <param name="mailAddress"></param>
  119. /// <param name="displayName"></param>
  120. public MailAddress(String mailAddress, String displayName) :
  121. this(mailAddress)
  122. {
  123. this._DisplayName = displayName;
  124. }
  125. private void InitializeProperty()
  126. {
  127. if (CultureInfo.CurrentCulture.Name.StartsWith("ja") == true)
  128. {
  129. this.Encoding = Encoding.GetEncoding("iso-2022-jp");
  130. this.TransferEncoding = TransferEncoding.Base64;
  131. }
  132. }
  133. /// <summary>
  134. ///
  135. /// </summary>
  136. /// <returns></returns>
  137. public override string ToString()
  138. {
  139. if (String.IsNullOrEmpty(this._DisplayName) == true)
  140. {
  141. return String.Format("<{0}>", this._Value);
  142. }
  143. if (this._IsDoubleQuote == true)
  144. {
  145. return String.Format("\"{0}\" <{1}>", this._DisplayName, this._Value);
  146. }
  147. else
  148. {
  149. return String.Format("{0} <{1}>", this._DisplayName, this._Value);
  150. }
  151. }
  152. /// <summary>
  153. ///
  154. /// </summary>
  155. /// <returns></returns>
  156. public String ToEncodeString()
  157. {
  158. return MailAddress.ToMailAddressText(this._Encoding, this._TransferEncoding
  159. , this._Value, this._DisplayName, this._IsDoubleQuote);
  160. }
  161. /// ???????????????????????????????????????????
  162. /// <summary>
  163. /// Get mail address text encoded by specify encoding.
  164. /// ???????????????????????????????????????????
  165. /// </summary>
  166. /// <param name="mailAddress"></param>
  167. /// <param name="displayName"></param>
  168. /// <param name="doubleQuote"></param>
  169. /// <returns></returns>
  170. public static String ToMailAddressText(String mailAddress, String displayName, Boolean doubleQuote)
  171. {
  172. if (CultureInfo.CurrentCulture.Name.StartsWith("ja") == true)
  173. {
  174. return MailAddress.ToMailAddressText(Encoding.GetEncoding("iso-2022-jp"), TransferEncoding.Base64
  175. , mailAddress, displayName, doubleQuote);
  176. }
  177. return MailAddress.ToMailAddressText(Encoding.ASCII, TransferEncoding.Base64, mailAddress, displayName, doubleQuote);
  178. }
  179. /// ???????????????????????????????????????????
  180. /// <summary>
  181. /// Get mail address text encoded by specify encoding.
  182. /// ???????????????????????????????????????????
  183. /// </summary>
  184. /// <param name="encoding"></param>
  185. /// <param name="transferEncoding"></param>
  186. /// <param name="mailAddress"></param>
  187. /// <param name="displayName"></param>
  188. /// <param name="doubleQuote"></param>
  189. /// <returns></returns>
  190. public static String ToMailAddressText(Encoding encoding, TransferEncoding transferEncoding
  191. , String mailAddress, String displayName, Boolean doubleQuote)
  192. {
  193. if (String.IsNullOrEmpty(displayName) == true)
  194. {
  195. return mailAddress;
  196. }
  197. else
  198. {
  199. if (doubleQuote == true)
  200. {
  201. return String.Format("\"{0}\" <{1}>", displayName, mailAddress);
  202. }
  203. else
  204. {
  205. return String.Format("{0} <{1}>"
  206. , MailParser.EncodeToMailHeaderLine(displayName, transferEncoding, encoding, MailParser.MaxCharCountPerRow - mailAddress.Length - 3)
  207. , mailAddress);
  208. }
  209. }
  210. }
  211. /// ??????MailAddress??????????????
  212. /// <summary>
  213. /// Create MailAddress object by mail address text.
  214. /// ??????MailAddress??????????????
  215. /// ???????????????????DisplayName?MailAddress?????????????????????
  216. /// ?????????????MailAddress?????????????????????????????
  217. /// </summary>
  218. /// <param name="mailAddress"></param>
  219. /// <returns></returns>
  220. public static MailAddress Create(String mailAddress)
  221. {
  222. Regex rx = RegexList.DisplayName_MailAddress;
  223. Match m = null;
  224. m = rx.Match(mailAddress);
  225. if (String.IsNullOrEmpty(m.Value) == true)
  226. {
  227. rx = RegexList.MailAddressWithBracket;
  228. m = rx.Match(mailAddress);
  229. if (String.IsNullOrEmpty(m.Value) == true)
  230. {
  231. return new MailAddress(mailAddress);
  232. }
  233. else
  234. {
  235. return new MailAddress(m.Groups["MailAddress"].Value);
  236. }
  237. }
  238. else
  239. {
  240. if (String.IsNullOrEmpty(m.Groups["DisplayName"].Value) == true)
  241. {
  242. return new MailAddress(mailAddress);
  243. }
  244. else
  245. {
  246. return new MailAddress(m.Groups["MailAddress"].Value, m.Groups["DisplayName"].Value.TrimEnd(' '));
  247. }
  248. }
  249. }
  250. /// ??????MailAddress????????????????
  251. /// <summary>
  252. /// Try to create MailAddress object by mail address text.
  253. /// ??????MailAddress????????????????
  254. /// ?????????????????????????null??????
  255. /// </summary>
  256. /// <param name="mailAddress"></param>
  257. /// <returns></returns>
  258. public static MailAddress TryCreate(String mailAddress)
  259. {
  260. try
  261. {
  262. if (String.IsNullOrEmpty(mailAddress) == true)
  263. { return null; }
  264. if (mailAddress.Contains("@") == false)
  265. { return null; }
  266. return MailAddress.Create(mailAddress);
  267. }
  268. catch { }
  269. return null;
  270. }
  271. /// ???????????????MailAddress??????????
  272. /// <summary>
  273. /// Get mailaddress list from mail address list text.
  274. /// ???????????????MailAddress??????????
  275. /// </summary>
  276. /// <param name="mailAddressListText"></param>
  277. /// <returns></returns>
  278. public static List<MailAddress> CreateMailAddressList(String mailAddressListText)
  279. {
  280. List<MailAddress> l = new List<MailAddress>();
  281. MailAddress m = null;
  282. String[] ss = null;
  283. ss = mailAddressListText.Split(',');
  284. for (int i = 0; i < ss.Length; i++)
  285. {
  286. m = MailAddress.TryCreate(ss[i].Trim());
  287. if (m == null)
  288. { continue; }
  289. l.Add(m);
  290. }
  291. return l;
  292. }
  293. }
  294. }