Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. public interface IEmailGenerator
  2. {
  3. MailMessage generateEmail(IMailObject mailObject, string htmlTemplate, string textTemplate);
  4. }
  5.  
  6. public class EmailGenerator : IEmailGenerator, IRegisterInIoC
  7. {
  8. private string mergeTemplate(string template, object obj)
  9. {
  10. Regex operationParser = new Regex(@"$(?:(?<operation>[w-,.]+)x20)(?<value>[w-,.]+)$", RegexOptions.Compiled);
  11. Regex valueParser = new Regex(@"$(?<value>[w-,.]+)$", RegexOptions.Compiled);
  12.  
  13. var operationMatches = operationParser.Matches(template).Cast<Match>().Reverse().ToList();
  14. foreach (var match in operationMatches)
  15. {
  16. string operation = match.Groups["operation"].Value;
  17. string value = match.Groups["value"].Value;
  18. var propertyInfo = obj.GetType().GetProperty(value);
  19. if (propertyInfo == null)
  20. throw new TillitException(String.Format("Could not find '{0}' in object of type '{1}'.", value, obj));
  21.  
  22. object dataValue = propertyInfo.GetValue(obj, null);
  23.  
  24. if (operation == "endforeach")
  25. {
  26. string foreachToken = "$foreach " + value + "$";
  27. var startIndex = template.LastIndexOf(foreachToken, match.Index);
  28. var templateBlock = template.Substring(startIndex + foreachToken.Length, match.Index - startIndex - foreachToken.Length);
  29. var items = (IEnumerable) value;
  30.  
  31. string blockResult = "";
  32.  
  33. foreach (object item in items)
  34. {
  35. blockResult += mergeTemplate(templateBlock, item);
  36. }
  37.  
  38. template = template.Remove(startIndex, match.Index - startIndex).Insert(startIndex, blockResult);
  39. }
  40. }
  41.  
  42. var valueMatches = valueParser.Matches(template).Cast<Match>().Reverse().ToList();
  43. foreach (var match in valueMatches)
  44. {
  45. string value = match.Groups["value"].Value;
  46. var propertyInfo = obj.GetType().GetProperty(value);
  47. if (propertyInfo == null)
  48. throw new Exception(String.Format("Could not find '{0}' in object of type '{1}'.", value, obj));
  49.  
  50. object dataValue = propertyInfo.GetValue(obj, null);
  51.  
  52. template = template.Remove(match.Index, match.Length).Insert(match.Index, dataValue.ToString());
  53. }
  54.  
  55. return template;
  56. }
  57.  
  58. public MailMessage generateEmail(IMailObject mailObject, string htmlTemplate, string textTemplate)
  59. {
  60. var mailMessage = new MailMessage();
  61. mailMessage.IsBodyHtml = true;
  62. mailMessage.Subject = mailObject.Subject;
  63. mailMessage.BodyEncoding = Encoding.UTF8;
  64.  
  65. // Create the Plain Text version of the email
  66. mailMessage.Body = this.mergeTemplate(textTemplate, mailObject);
  67.  
  68. // Create the HTML version of the email
  69. ContentType mimeType = new System.Net.Mime.ContentType("text/html");
  70.  
  71. AlternateView alternate = AlternateView.CreateAlternateViewFromString(this.mergeTemplate(htmlTemplate, mailObject), mimeType);
  72. mailMessage.AlternateViews.Add(alternate);
  73.  
  74. return mailMessage;
  75. }
  76. }
  77.  
  78. public class MessageData : IMailObject
  79. {
  80. public string Property1 { get; private set; }
  81. public string Property2 { get; private set; }
  82. public string Property3 { get; private set; }
  83. public string Property4 { get; private set; }
  84. public string Property5 { get; private set; }
  85. public string Property6 { get; private set; }
  86. public string Subject
  87. {
  88. get { return this.Property1 + DateTime.Now.ToShortDateString(); }
  89. }
  90. public List<MessageItemData> Items { get; private set; }
  91.  
  92. public MessageData(string property1, string property2, string property3, DateTime property4, string property7, string property8, DateTime property9, DateTime property10, int property11, double property12, string property5, string property6)
  93. {
  94. this.Property1 = property1;
  95. this.Property2 = property2;
  96. this.Property3 = property3;
  97. this.Property4 = property4.ToShortDateString();
  98. this.Property5 = property5;
  99. this.Property6 = property6;
  100.  
  101. this.Items = new List<MessageItemData>();
  102. this.Items.Add(new MessageItemData(property7, property8, property9, property10, property11, property12));
  103. }
  104. }
  105.  
  106. public class MessageItemData
  107. {
  108. public string Property7 { get; private set; }
  109. public string Property8 { get; private set; }
  110. public string Property9 { get; private set; }
  111. public string Property10 { get; private set; }
  112. public int Property11 { get; private set; }
  113. public double Property12 { get; private set; }
  114.  
  115. public MessageItemData( string property7, string property8, DateTime property9, DateTime property10, int property11, double property12)
  116. {
  117. this.Property7 = property7;
  118. this.Property8 = property8;
  119. this.Property9 = property9.ToShortDateString();
  120. this.Property10 = property10.ToShortDateString();
  121. this.Property11 = property11;
  122. this.Property12 = property12;
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement