PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenIdProvider/Helpers/Email.cs

https://bitbucket.org/sparktree/stackexchange.stackid
C# | 98 lines | 55 code | 11 blank | 32 comment | 2 complexity | edf748b20a18308e6e2934234bcb73bb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Net.Mail;
  6. using System.Net;
  7. using MarkdownSharp;
  8. using System.IO;
  9. using System.Text;
  10. using MvcMiniProfiler;
  11. namespace OpenIdProvider.Helpers
  12. {
  13. /// <summary>
  14. /// When dealing with multiple concrete Email implementations,
  15. /// Current will use this attribute to determine which one to actually instantiate.
  16. ///
  17. /// The highest priority (where 2 has a higher priority than 1) wins.
  18. /// </summary>
  19. public class PriorityAttribute : Attribute
  20. {
  21. public int Priority { get; set; }
  22. public PriorityAttribute(int priority) { Priority = priority; }
  23. }
  24. /// <summary>
  25. /// Helper class for sending emails.
  26. ///
  27. /// Abstracts away all the nasty SMTP and
  28. /// message formatting non-sense.
  29. /// </summary>
  30. public abstract class Email
  31. {
  32. public enum Template
  33. {
  34. AffiliateRegistered,
  35. CompleteRegistration,
  36. CompleteRegistrationViaAffiliate,
  37. PasswordChanged,
  38. ResetPassword,
  39. ResetPasswordAffiliate
  40. }
  41. /// <summary>
  42. /// Returns an email template, with all the {Names} replaced with the corresponding properties on params and some
  43. /// default "SiteWide" ones like {SiteName}.
  44. ///
  45. /// Also pushes an appropriate subject into subject.
  46. /// </summary>
  47. private static string GetEmailText(string templateName, object @params, out string subject, out string textVersion)
  48. {
  49. using (MiniProfiler.Current.Step("GetEmailText"))
  50. {
  51. var markdown = Helpers.Template.FormatTemplate(templateName, @params).Trim();
  52. int i = markdown.IndexOf('\n');
  53. subject = markdown.Substring(0, i + 1).Trim();
  54. textVersion = markdown.Substring(i + 1).Trim();
  55. return (new Markdown()).Transform(textVersion);
  56. }
  57. }
  58. /// <summary>
  59. /// Sends an email.
  60. ///
  61. /// cc and bcc accept semicolon delimited lists of addresses.
  62. /// </summary>
  63. public bool SendEmail(string to, Template templateName, object @params = null, string cc = null, string bcc = null)
  64. {
  65. using (MiniProfiler.Current.Step("SendEmail"))
  66. {
  67. var ccList = new List<string>();
  68. var bccList = new List<string>();
  69. if (cc.HasValue()) ccList.AddRange(cc.Split(';'));
  70. if (bcc.HasValue()) bccList.AddRange(bcc.Split(';'));
  71. string subject, textMessage;
  72. var htmlMessage = GetEmailText(Enum.GetName(typeof(Template), templateName), @params, out subject, out textMessage);
  73. return SendEmailImpl(to, ccList, bccList, subject, htmlMessage, textMessage);
  74. }
  75. }
  76. /// <summary>
  77. /// Actual implementation of sending an email.
  78. ///
  79. /// Concrete implementations of this method must be thread safe.
  80. ///
  81. /// This whole song and dance is to enable complete different implementations of email
  82. /// to be swapped out easily. SE Inc. relies on some third-party, closed source, mailing
  83. /// libraries.
  84. /// </summary>
  85. protected abstract bool SendEmailImpl(string to, IEnumerable<string> cc, IEnumerable<string> bcc, string title, string bodyHtml, string bodyText);
  86. }
  87. }