/Src/Bowerbird.Core/Utilities/Email.cs

https://github.com/Bowerbird/bowerbird-web · C# · 379 lines · 206 code · 40 blank · 133 comment · 11 complexity · 8274263ec94e325fcc607a8080a00232 MD5 · raw file

  1. /* Bowerbird V1 - Licensed under MIT 1.1 Public License
  2. Developers:
  3. * Frank Radocaj : frank@radocaj.com
  4. * Hamish Crittenden : hamish.crittenden@gmail.com
  5. Project Manager:
  6. * Ken Walker : kwalker@museum.vic.gov.au
  7. Funded by:
  8. * Atlas of Living Australia
  9. */
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net.Mail;
  15. using System.Reflection;
  16. using Nustache.Core;
  17. namespace Bowerbird.Core.Utilities
  18. {
  19. public class Email : IDisposable
  20. {
  21. private SmtpClient _client;
  22. private bool? _useSsl;
  23. public MailMessage Message { get; set; }
  24. private Email()
  25. {
  26. Message = new MailMessage();
  27. _client = new SmtpClient();
  28. }
  29. /// <summary>
  30. /// Creates a new email instance using the default from
  31. /// address from smtp config settings
  32. /// </summary>
  33. /// <returns>Instance of the Email class</returns>
  34. public static Email FromDefault()
  35. {
  36. var email = new Email
  37. {
  38. Message = new MailMessage()
  39. };
  40. return email;
  41. }
  42. /// <summary>
  43. /// Creates a new Email instance and sets the from
  44. /// property
  45. /// </summary>
  46. /// <param name="emailAddress">Email address to send from</param>
  47. /// <param name="name">Name to send from</param>
  48. /// <returns>Instance of the Email class</returns>
  49. public static Email From(string emailAddress, string name = "")
  50. {
  51. var email = new Email
  52. {
  53. Message = { From = new MailAddress(emailAddress, name) }
  54. };
  55. return email;
  56. }
  57. /// <summary>
  58. /// Adds a reciepient to the email, Splits name and address on ';'
  59. /// </summary>
  60. /// <param name="emailAddress">Email address of recipeient</param>
  61. /// <param name="name">Name of recipient</param>
  62. /// <returns>Instance of the Email class</returns>
  63. public Email To(string emailAddress, string name)
  64. {
  65. if (emailAddress.Contains(";"))
  66. {
  67. //email address has semi-colon, try split
  68. var nameSplit = name.Split(';');
  69. var addressSplit = emailAddress.Split(';');
  70. for (int i = 0; i < addressSplit.Length; i++)
  71. {
  72. var currentName = string.Empty;
  73. if ((nameSplit.Length - 1) >= i)
  74. {
  75. currentName = nameSplit[i];
  76. }
  77. Message.To.Add(new MailAddress(addressSplit[i], currentName));
  78. }
  79. }
  80. else
  81. {
  82. Message.To.Add(new MailAddress(emailAddress, name));
  83. }
  84. return this;
  85. }
  86. /// <summary>
  87. /// Adds a reciepient to the email
  88. /// </summary>
  89. /// <param name="emailAddress">Email address of recipeient (allows multiple splitting on ';')</param>
  90. /// <returns></returns>
  91. public Email To(string emailAddress)
  92. {
  93. if (emailAddress.Contains(";"))
  94. {
  95. foreach (string address in emailAddress.Split(';'))
  96. {
  97. Message.To.Add(new MailAddress(address));
  98. }
  99. }
  100. else
  101. {
  102. Message.To.Add(new MailAddress(emailAddress));
  103. }
  104. return this;
  105. }
  106. /// <summary>
  107. /// Adds all reciepients in list to email
  108. /// </summary>
  109. /// <param name="mailAddresses">List of recipients</param>
  110. /// <returns>Instance of the Email class</returns>
  111. public Email To(IList<MailAddress> mailAddresses)
  112. {
  113. foreach (var address in mailAddresses)
  114. {
  115. Message.To.Add(address);
  116. }
  117. return this;
  118. }
  119. /// <summary>
  120. /// Adds a Carbon Copy to the email
  121. /// </summary>
  122. /// <param name="emailAddress">Email address to cc</param>
  123. /// <param name="name">Name to cc</param>
  124. /// <returns>Instance of the Email class</returns>
  125. public Email CC(string emailAddress, string name = "")
  126. {
  127. Message.CC.Add(new MailAddress(emailAddress, name));
  128. return this;
  129. }
  130. /// <summary>
  131. /// Adds all Carbon Copy in list to an email
  132. /// </summary>
  133. /// <param name="mailAddresses">List of recipients to CC</param>
  134. /// <returns>Instance of the Email class</returns>
  135. public Email CC(IList<MailAddress> mailAddresses)
  136. {
  137. foreach (var address in mailAddresses)
  138. {
  139. Message.CC.Add(address);
  140. }
  141. return this;
  142. }
  143. /// <summary>
  144. /// Adds a blind carbon copy to the email
  145. /// </summary>
  146. /// <param name="emailAddress">Email address of bcc</param>
  147. /// <param name="name">Name of bcc</param>
  148. /// <returns>Instance of the Email class</returns>
  149. public Email BCC(string emailAddress, string name = "")
  150. {
  151. Message.Bcc.Add(new MailAddress(emailAddress, name));
  152. return this;
  153. }
  154. /// <summary>
  155. /// Adds all blind carbon copy in list to an email
  156. /// </summary>
  157. /// <param name="mailAddresses">List of recipients to BCC</param>
  158. /// <returns>Instance of the Email class</returns>
  159. public Email BCC(IList<MailAddress> mailAddresses)
  160. {
  161. foreach (var address in mailAddresses)
  162. {
  163. Message.Bcc.Add(address);
  164. }
  165. return this;
  166. }
  167. /// <summary>
  168. /// Sets the ReplyTo address on the email
  169. /// </summary>
  170. /// <param name="address">The ReplyTo Address</param>
  171. /// <returns></returns>
  172. public Email ReplyTo(string address)
  173. {
  174. Message.ReplyToList.Add(new MailAddress(address));
  175. return this;
  176. }
  177. /// <summary>
  178. /// Sets the ReplyTo address on the email
  179. /// </summary>
  180. /// <param name="address">The ReplyTo Address</param>
  181. /// <param name="name">The Display Name of the ReplyTo</param>
  182. /// <returns></returns>
  183. public Email ReplyTo(string address, string name)
  184. {
  185. Message.ReplyToList.Add(new MailAddress(address, name));
  186. return this;
  187. }
  188. /// <summary>
  189. /// Sets the subject of the email
  190. /// </summary>
  191. /// <param name="subject">email subject</param>
  192. /// <returns>Instance of the Email class</returns>
  193. public Email Subject(string subject)
  194. {
  195. Message.Subject = subject;
  196. return this;
  197. }
  198. /// <summary>
  199. /// Adds a Body to the Email
  200. /// </summary>
  201. /// <param name="body">The content of the body</param>
  202. /// <param name="isHtml">True if Body is HTML, false for plain text (Optional)</param>
  203. /// <returns></returns>
  204. public Email Body(string body, bool isHtml = true)
  205. {
  206. Message.Body = body;
  207. Message.IsBodyHtml = isHtml;
  208. return this;
  209. }
  210. /// <summary>
  211. /// Marks the email as High Priority
  212. /// </summary>
  213. /// <returns></returns>
  214. public Email HighPriority()
  215. {
  216. Message.Priority = MailPriority.High;
  217. return this;
  218. }
  219. /// <summary>
  220. /// Marks the email as Low Priority
  221. /// </summary>
  222. /// <returns></returns>
  223. public Email LowPriority()
  224. {
  225. Message.Priority = MailPriority.Low;
  226. return this;
  227. }
  228. /// <summary>
  229. /// Adds razor template to the email
  230. /// </summary>
  231. /// <param name="templateName">The name of the template</param>
  232. /// <param name="isHtml">True if Body is HTML, false for plain text (Optional)</param>
  233. /// <returns>Instance of the Email class</returns>
  234. public Email UsingTemplate<T>(string templateName, T model, bool isHtml = true)
  235. {
  236. string template = null;
  237. Assembly assembly = Assembly.GetAssembly(typeof (Email));
  238. using (Stream stream = assembly.GetManifestResourceStream(string.Format("Bowerbird.Core.EmailTemplates.{0}.mustache", templateName)))
  239. {
  240. using (var reader = new StreamReader(stream))
  241. {
  242. template = reader.ReadToEnd();
  243. }
  244. }
  245. var body = Render.StringToString(template, model);
  246. Message.Body = body;
  247. Message.IsBodyHtml = isHtml;
  248. return this;
  249. }
  250. /// <summary>
  251. /// Adds an Attachment to the Email
  252. /// </summary>
  253. /// <param name="attachment">The Attachment to add</param>
  254. /// <returns>Instance of the Email class</returns>
  255. public Email Attach(Attachment attachment)
  256. {
  257. if (!Message.Attachments.Contains(attachment))
  258. Message.Attachments.Add(attachment);
  259. return this;
  260. }
  261. /// <summary>
  262. /// Adds Multiple Attachments to the Email
  263. /// </summary>
  264. /// <param name="attachments">The List of Attachments to add</param>
  265. /// <returns>Instance of the Email class</returns>
  266. public Email Attach(IList<Attachment> attachments)
  267. {
  268. foreach (var attachment in attachments.Where(attachment => !Message.Attachments.Contains(attachment)))
  269. {
  270. Message.Attachments.Add(attachment);
  271. }
  272. return this;
  273. }
  274. /// <summary>
  275. /// Over rides the default client from .config file
  276. /// </summary>
  277. /// <param name="client">Smtp client to send from</param>
  278. /// <returns>Instance of the Email class</returns>
  279. public Email UsingClient(SmtpClient client)
  280. {
  281. _client = client;
  282. return this;
  283. }
  284. public Email UseSSL()
  285. {
  286. _useSsl = true;
  287. return this;
  288. }
  289. /// <summary>
  290. /// Sends email synchronously
  291. /// </summary>
  292. /// <returns>Instance of the Email class</returns>
  293. public Email Send()
  294. {
  295. if (_useSsl.HasValue)
  296. _client.EnableSsl = _useSsl.Value;
  297. _client.Send(Message);
  298. return this;
  299. }
  300. /// <summary>
  301. /// Sends message asynchronously with a callback
  302. /// handler
  303. /// </summary>
  304. /// <param name="callback">Method to call on complete</param>
  305. /// <param name="token">User token to pass to callback</param>
  306. /// <returns>Instance of the Email class</returns>
  307. public Email SendAsync(SendCompletedEventHandler callback, object token = null)
  308. {
  309. if (_useSsl.HasValue)
  310. _client.EnableSsl = _useSsl.Value;
  311. _client.SendCompleted += callback;
  312. _client.SendAsync(Message, token);
  313. return this;
  314. }
  315. /// <summary>
  316. /// Cancels async message sending
  317. /// </summary>
  318. /// <returns>Instance of the Email class</returns>
  319. public Email Cancel()
  320. {
  321. _client.SendAsyncCancel();
  322. return this;
  323. }
  324. /// <summary>
  325. /// Releases all resources
  326. /// </summary>
  327. public void Dispose()
  328. {
  329. if (_client != null)
  330. _client.Dispose();
  331. if (Message != null)
  332. Message.Dispose();
  333. }
  334. }
  335. }