Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. private static void Main(string[] args)
  2. {
  3. Sender.Errors.Clear();
  4. Sender.SendWelcomeEmails();
  5. SendingMail.SendComeBackEmail("Thisisavouchercode");
  6. if (sender.Errors.Any())
  7. Console.WriteLine("All mails are sent, I hope...");
  8. }
  9.  
  10. public class Customer
  11. {
  12. public string Email { get; set; }
  13. public DateTime CreatedDateTime { get; set; }
  14. }
  15.  
  16. public class Order
  17. {
  18. public string CustomerEmail { get; set; }
  19. public DateTime OrderDatetime { get; set; }
  20. }
  21.  
  22. class DataLayer
  23. {
  24. /// <summary>
  25. /// Mockup method for all customers
  26. /// </summary>
  27. public static List<Customer> ListCustomers()
  28. {
  29. return new List<Customer>()
  30. {
  31. new Customer(){Email = "mail1@mail.com", CreatedDateTime = DateTime.Now.AddHours(-7)},
  32. new Customer(){Email = "mail2@mail.com", CreatedDateTime = DateTime.Now.AddDays(-1)},
  33. new Customer(){Email = "mail3@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-6)},
  34. new Customer(){Email = "mail4@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-1)},
  35. new Customer(){Email = "mail5@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-2)},
  36. new Customer(){Email = "mail6@mail.com", CreatedDateTime = DateTime.Now.AddDays(-5)}
  37. };
  38. }
  39.  
  40. /// <summary>
  41. /// Mockup method for listing all orders
  42. /// </summary>
  43. public static List<Order> ListOrders()
  44. {
  45. return new List<Order>()
  46. {
  47. new Order(){CustomerEmail = "mail3@mail.com", OrderDatetime = DateTime.Now.AddMonths(-6)},
  48. new Order(){CustomerEmail = "mail5@mail.com", OrderDatetime = DateTime.Now.AddMonths(-2)},
  49. new Order(){CustomerEmail = "mail6@mail.com", OrderDatetime = DateTime.Now.AddDays(-2)}
  50. };
  51. }
  52. }
  53.  
  54. public interface IMailSender
  55. {
  56. void Send(string from, string to, string body);
  57. }
  58. sealed class NullMailSender : IMailSender
  59. {
  60. void IMailSender.Send(string from, string to, string body)
  61. {
  62.  
  63. }
  64. }
  65.  
  66. sealed class SmtpMailSender : IMailSender
  67. {
  68. void IMailSender.Send(string from, string to, string body)
  69. {
  70. System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
  71. mail.From = new System.Net.Mail.MailAddress(from);
  72. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
  73. mail.To.Add(to);
  74. mail.Body = body;
  75. smtp.Send(mail);
  76.  
  77. }
  78. }
  79.  
  80. public class SendingMail
  81. {
  82. public List<string> Errors { get; } = new List<string>();
  83.  
  84. public IEnumerable<Customer> Customers { get; set; }
  85.  
  86. public IEnumerable<Order> Orders { get; set; }
  87.  
  88. public IMailSender Sender { get; set; }
  89.  
  90. public void SendWelcomeEmails()
  91. {
  92. var template = Resources.WelcomeEmailTemplate;
  93. Send(GetNewCustomers(), Resources.WelcomeEmailTemplate);
  94. }
  95.  
  96. public void SendComeBackEmail()
  97. {
  98. var template = Resources.WelcomeEmailTemplate;
  99. var emailContent = String.Format(template);
  100. Send(GetCustomersWithoutRecentOrders(), Resources.ComeBackEmailTemplate);
  101. }
  102.  
  103. private IEnumerable<Customer> GetNewCustomers()
  104. {
  105. var yesterday = DateTime.Now.Date.AddDays(-1);
  106. return Customers.Where(x => x.CreatedDateTime >= yesterday);
  107. }
  108. private IEnumerable<Customer> GetCustomersWithoutRecentOrders()
  109. {
  110. var oneMonthAgo = DateTime.Now.Date.AddMonths(-1);
  111.  
  112. return Customers.Where(c => {
  113. var latestOrder = Orders
  114. .Where(o => o.CustomerEmail == c.Email)
  115. .OrderByDescending(o => o.OrderDatetime)
  116. .FirstOrDefault();
  117.  
  118. return latestOrder != null
  119. && latestOrder.OrderDatetime < oneMonthAgo;
  120. });
  121. }
  122.  
  123. private void Send(IEnumerable<Customer> customers, string body)
  124. {
  125. foreach (Customer customer in customers)
  126. Send(customer, body);
  127. }
  128. private void Send(Customer customer, string body)
  129. {
  130. MailMessage message = GenerateMessage(customer, body);
  131. int NumberOfRetriesOnError = 2;
  132. int DelayOnError = 10;
  133. for (int i = 0; i <= NumberOfRetriesOnError; ++i)
  134. {
  135. try
  136. {
  137. Sender.Send(GenerateMessage);
  138. return;
  139. }
  140. catch (SmtpException e)
  141. {
  142. if (i < NumberOfRetriesOnError)
  143. Thread.Sleep((i + 1) * DelayOnError);
  144. else
  145. Errors.Add(e.Message + customer.Email); // Include customeremail
  146. }
  147. }
  148. }
  149. private MailMessage GenerateMessage(Customer customer, string body)
  150. {
  151. string subject = "...";
  152. string from = "...";
  153. string to = customer.Email;
  154. MailMessage retVal = new MailMessage(from, to, subject, body);
  155. return retVal;
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement