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

/SimpleUtils/SimpleMail.cs

http://simple-assembly-explorer.googlecode.com/
C# | 314 lines | 153 code | 49 blank | 112 comment | 24 complexity | 3460829576e53a4585a8863ac7278d55 MD5 | raw file
Possible License(s): GPL-3.0, MIT, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Mail;
  6. using System.Net.Sockets;
  7. using Microsoft.Win32;
  8. namespace SimpleUtils
  9. {
  10. public class SimpleMail
  11. {
  12. #region Properties
  13. private string _smtpServer = null;
  14. public string SmtpServer
  15. {
  16. get { return _smtpServer; }
  17. }
  18. private string _userName = null;
  19. public string UserName
  20. {
  21. get { return _userName; }
  22. set { _userName = value; }
  23. }
  24. private string _password = null;
  25. public string Password
  26. {
  27. get { return _password; }
  28. set { _password = value; }
  29. }
  30. private bool _useDefaultCredentials = false;
  31. public bool UseDefaultCredentials
  32. {
  33. get { return _useDefaultCredentials; }
  34. set { _useDefaultCredentials = value; }
  35. }
  36. private int _port = 0;
  37. public int Port
  38. {
  39. get { return _port; }
  40. set { _port = value; }
  41. }
  42. private MailAddress _fromAddress = null;
  43. public MailAddress FromAddress
  44. {
  45. get { return _fromAddress; }
  46. set { _fromAddress = value; }
  47. }
  48. #endregion Properties
  49. #region Constructors
  50. public SimpleMail(string smtpServer, string fromAddress)
  51. {
  52. Init(smtpServer, fromAddress, null);
  53. }
  54. public SimpleMail(string smtpServer, string fromAddress, string fromDisplayName)
  55. {
  56. Init(smtpServer, fromAddress, fromDisplayName);
  57. }
  58. protected void Init(string smtpServer, string fromAddress, string fromDisplayName)
  59. {
  60. _smtpServer = smtpServer;
  61. _fromAddress = new MailAddress(fromAddress, fromDisplayName);
  62. }
  63. #endregion Constructors
  64. #region Send Mail Functions
  65. public void Send(string receipt, string receiptName, string subject, string body)
  66. {
  67. Send(receipt, receiptName, subject, body, null, null, null);
  68. }
  69. public void Send(string receipt, string receiptName, string subject, string body, string[] attachments)
  70. {
  71. Send(receipt, receiptName, subject, body, null, null, attachments);
  72. }
  73. public void Send(string receipt, string receiptName, string subject, string body, string cc)
  74. {
  75. Send(receipt, receiptName, subject, body, cc, null, null);
  76. }
  77. public void Send(string receipt, string receiptName, string subject, string body, string cc, string[] attachments)
  78. {
  79. Send(receipt, receiptName, subject, body, cc, null, attachments);
  80. }
  81. public void Send(string receipt, string receiptName, string subject, string body, string cc, string bcc, string[] attachments)
  82. {
  83. MailMessage msg = new MailMessage();
  84. msg.From = _fromAddress;
  85. AddAddresses(receipt, receiptName, msg.To);
  86. AddAddresses(cc, msg.CC);
  87. AddAddresses(bcc, msg.Bcc);
  88. msg.Subject = subject;
  89. msg.BodyEncoding = System.Text.Encoding.UTF8;
  90. msg.IsBodyHtml = true;
  91. msg.Body = body;
  92. if (attachments != null)
  93. {
  94. for (int i = 0; i < attachments.Length; i++)
  95. {
  96. if (String.IsNullOrEmpty(attachments[i])) continue;
  97. msg.Attachments.Add(new Attachment(attachments[i]));
  98. }
  99. }
  100. SmtpClient smtpClient = new SmtpClient(_smtpServer);
  101. if (_port > 0) smtpClient.Port = _port;
  102. if (IsAuthenticationRequired)
  103. {
  104. CredentialCache myCache = new CredentialCache();
  105. myCache.Add(_smtpServer, smtpClient.Port, "login", new NetworkCredential(UserName, Password));
  106. smtpClient.Credentials = myCache;
  107. }
  108. else
  109. {
  110. if (_useDefaultCredentials) smtpClient.UseDefaultCredentials = _useDefaultCredentials;
  111. }
  112. smtpClient.Send(msg);
  113. }
  114. private bool IsAuthenticationRequired
  115. {
  116. get { return !String.IsNullOrEmpty(UserName) && !String.IsNullOrEmpty(Password); }
  117. }
  118. private char[] _addressSeparator = new char[] { ';' };
  119. protected void AddAddresses(string addresses, MailAddressCollection mac)
  120. {
  121. if (addresses == null) return;
  122. string[] s = addresses.Split(_addressSeparator, StringSplitOptions.RemoveEmptyEntries);
  123. if (s != null)
  124. {
  125. for (int i = 0; i < s.Length; i++)
  126. {
  127. mac.Add(s[i]);
  128. }
  129. }
  130. }
  131. protected void AddAddresses(string addresses, string displayNames, MailAddressCollection mac)
  132. {
  133. if (addresses == null) return;
  134. string[] addrs = addresses.Split(_addressSeparator, StringSplitOptions.RemoveEmptyEntries);
  135. string[] names;
  136. if (String.IsNullOrEmpty(displayNames)) names = null;
  137. else names = displayNames.Split(_addressSeparator, StringSplitOptions.RemoveEmptyEntries);
  138. if (addrs != null)
  139. {
  140. for (int i = 0; i < addrs.Length; i++)
  141. {
  142. string address = addrs[i].Trim();
  143. string name;
  144. if (names != null && i < names.Length) name = names[i];
  145. else name = address;
  146. MailAddress addr = new MailAddress(address, name);
  147. mac.Add(addr);
  148. }
  149. }
  150. }
  151. #endregion Send Mail Functions
  152. //#region Email Checking functions
  153. //public bool IsValidEmailAddress(string emailAddress)
  154. //{
  155. // if (!SimpleRegex.IsEmail(emailAddress))
  156. // {
  157. // return false;
  158. // }
  159. // bool valid = false;
  160. // try
  161. // {
  162. // using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  163. // {
  164. // //StringBuilder sb = new StringBuilder();
  165. // int timeout = 10000;
  166. // socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout);
  167. // int port = 25;
  168. // socket.Connect(_smtpServer, port);
  169. // byte[] buffer = new byte[0x400];
  170. // int count = socket.Receive(buffer);
  171. // string result = Encoding.ASCII.GetString(buffer, 0, count);
  172. // //sb.Append(result);
  173. // string command = String.Format("HELO {0}\r\n", SimpleSystemInfo.SimpleNetworkInfo.GetHostName());
  174. // //sb.Append(command);
  175. // byte[] bytes = Encoding.ASCII.GetBytes(command);
  176. // socket.Send(bytes);
  177. // count = socket.Receive(buffer);
  178. // result = Encoding.ASCII.GetString(buffer, 0, count);
  179. // //sb.Append(result);
  180. // command = String.Format("MAIL FROM:<{0}>\r\n", _fromAddress);
  181. // //sb.Append(command);
  182. // bytes = Encoding.ASCII.GetBytes(command);
  183. // socket.Send(bytes);
  184. // count = socket.Receive(buffer);
  185. // result = Encoding.ASCII.GetString(buffer, 0, count);
  186. // //sb.Append(result);
  187. // command = String.Format("RCPT TO:<{0}>\r\n", emailAddress.Trim());
  188. // //sb.Append(command);
  189. // bytes = Encoding.ASCII.GetBytes(command);
  190. // socket.Send(bytes);
  191. // count = socket.Receive(buffer);
  192. // result = Encoding.ASCII.GetString(buffer, 0, count);
  193. // //sb.Append(result);
  194. // bool flag = this.IsGoodResponse(result);
  195. // command = "QUIT\r\n";
  196. // //sb.Append(command);
  197. // bytes = Encoding.ASCII.GetBytes(command);
  198. // socket.Send(bytes);
  199. // count = socket.Receive(buffer);
  200. // result = Encoding.ASCII.GetString(buffer, 0, count);
  201. // //sb.Append(result);
  202. // valid = flag;
  203. // }
  204. // }
  205. // catch
  206. // {
  207. // throw;
  208. // }
  209. // return valid;
  210. //}
  211. //private bool IsGoodResponse(string response)
  212. //{
  213. // return response.Trim().StartsWith("250");
  214. //}
  215. //#endregion Email Checking functions
  216. //#region Internet Accounts Manager
  217. //public class InternetEmailAccount
  218. //{
  219. // public string POP3Server;
  220. // public string SMTPServer;
  221. // public string SMTPEmailAddress;
  222. // public string SMTPDisplayName;
  223. //}
  224. //public static InternetEmailAccount GetInternetEmailAccount()
  225. //{
  226. // string defaultEntry = GetInternetEmailAccountDefaultEntry();
  227. // if (String.IsNullOrEmpty(defaultEntry)) return null;
  228. // return GetInternetEmailAccount(defaultEntry);
  229. //}
  230. //public static InternetEmailAccount GetInternetEmailAccount(string entry)
  231. //{
  232. // InternetEmailAccount account = null;
  233. // try
  234. // {
  235. // using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Account Manager\Accounts\" + entry))
  236. // {
  237. // if (key != null)
  238. // {
  239. // account = new InternetEmailAccount();
  240. // account.POP3Server = key.GetValue("POP3 Server", null) as string;
  241. // account.SMTPServer = key.GetValue("SMTP Server", null) as string;
  242. // account.SMTPEmailAddress = key.GetValue("SMTP Email Address", null) as string;
  243. // account.SMTPDisplayName = key.GetValue("SMTP Display Name", null) as string;
  244. // }
  245. // }
  246. // }
  247. // catch
  248. // {
  249. // return null;
  250. // }
  251. // return account;
  252. //}
  253. //private static string GetInternetEmailAccountDefaultEntry()
  254. //{
  255. // string s = null;
  256. // using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Account Manager"))
  257. // {
  258. // if (key != null)
  259. // s = key.GetValue("Default Mail Account", null) as string;
  260. // }
  261. // return s;
  262. //}
  263. //#endregion Internet Accounts Manager
  264. } //end of class
  265. }