PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/uComponents.XsltExtensions/Email.cs

https://github.com/s6admin/uComponents
C# | 60 lines | 37 code | 3 blank | 20 comment | 1 complexity | 9e25ae5192d838fda6baafbcff201c3e MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. using System;
  2. using System.Net.Mail;
  3. using System.Text.RegularExpressions;
  4. using umbraco;
  5. using Umbraco.Core.Logging;
  6. namespace uComponents.XsltExtensions
  7. {
  8. /// <summary>
  9. /// The Email class exposes XSLT extensions to offer extended email functionality.
  10. /// </summary>
  11. [XsltExtension("ucomponents.email")]
  12. public class Email
  13. {
  14. /// <summary>
  15. /// Determines whether [is valid email] [the specified input].
  16. /// </summary>
  17. /// <param name="input">The input.</param>
  18. /// <returns>
  19. /// <c>true</c> if [is valid email] [the specified input]; otherwise, <c>false</c>.
  20. /// </returns>
  21. /// <remarks>http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx</remarks>
  22. public static bool IsValidEmail(string input)
  23. {
  24. var pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
  25. var regex = new Regex(pattern, RegexOptions.IgnoreCase);
  26. return regex.IsMatch(input);
  27. }
  28. /// <summary>
  29. /// Sends an email. Performs the same function an <c>umbraco.library.SendMail</c>, with the option of sending via SSL.
  30. /// </summary>
  31. /// <param name="from">The 'from' email address.</param>
  32. /// <param name="to">The 'to' email address.</param>
  33. /// <param name="subject">The subject of the email.</param>
  34. /// <param name="body">The content body of the email.</param>
  35. /// <param name="isHtml">If set to <c>true</c>, then content body is HTML, otherwise plain-text.</param>
  36. /// <param name="useSSL">If set to <c>true</c>, then use SSL, otherwise use non-secure protocol.</param>
  37. public static void SendMail(string from, string to, string subject, string body, bool isHtml, bool useSSL)
  38. {
  39. if (!useSSL)
  40. {
  41. library.SendMail(from, to, subject, body, isHtml);
  42. return;
  43. }
  44. try
  45. {
  46. var message = new MailMessage(from, to, subject, body) { IsBodyHtml = isHtml };
  47. var client = new SmtpClient();
  48. client.EnableSsl = true;
  49. client.Send(message);
  50. }
  51. catch (Exception ex)
  52. {
  53. LogHelper.Error(typeof(Email), "uComponents.XsltExtensions.Email.SendMail: Error sending mail.", ex);
  54. }
  55. }
  56. }
  57. }