EmailTemplateFactory.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections.Concurrent;
  2. using System.Collections.Generic;
  3. namespace FlyweightPattern
  4. {
  5. public static class EmailTemplateFactory
  6. {
  7. /// <summary>
  8. /// 预置模板
  9. /// </summary>
  10. private static readonly Dictionary<string, string> SubjectAndContentMapping = new Dictionary<string, string>()
  11. {
  12. {
  13. "待修复漏洞通知",
  14. @"尊敬的用户:云盾检测到您的服务器存在phpwindv9任务中心GET型CSRF代码执行漏洞,
  15. 目前已为您研发了漏洞补丁,可在云盾控制台进行一键修复。为避免该漏洞被黑客利用,
  16. 建议您尽快修复该漏洞。您可以点击此处登录云盾 - 服务器安全(安骑士)控制台进行查看和修复"
  17. },
  18. {
  19. "阿里云ECS即将到期通知",
  20. @"您有1台云服务器ECS将于一周后正式到期。未续费的云服务器ECS实例到期后将停止服务,
  21. 到期后数据为您保留7天,逾期未续费实例与磁盘会被释放,数据不可恢复。
  22. 为了保证您的服务正常运行,请及时续费。"
  23. },
  24. {"阿里云故障通告", "您的服务器存在故障,请您了解!"},
  25. {"阿里云升级通知", "我们将对阿里云进行升级,会存在服务器短暂不可用情况,请知悉!"}
  26. };
  27. /// <summary>
  28. /// 定义对象池
  29. /// </summary>
  30. static readonly ConcurrentDictionary<string, Email> EmailTemplates = new ConcurrentDictionary<string, Email>();
  31. /// <summary>
  32. /// 根据主题获取模板
  33. /// </summary>
  34. /// <param name="subject"></param>
  35. /// <returns></returns>
  36. public static Email GetTemplate(string subject)
  37. {
  38. Email email = null;
  39. if (!EmailTemplates.ContainsKey(subject))
  40. {
  41. string template;
  42. SubjectAndContentMapping.TryGetValue(subject, out template);
  43. email = new Email("[email protected]", subject, string.IsNullOrWhiteSpace(template) ? subject : template, "阿里云计算公司");
  44. EmailTemplates.TryAdd(subject, email);
  45. }
  46. else
  47. {
  48. EmailTemplates.TryGetValue(subject, out email);
  49. }
  50. return email;
  51. }
  52. }
  53. }