Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 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.  
  55. public interface IMailSender
  56. {
  57. void Send(string from, string to, string body);
  58. }
  59. sealed class NullMailSender : IMailSender
  60. {
  61. void IMailSender.Send(string from, string to, string body)
  62. {
  63.  
  64. }
  65. }
  66.  
  67. sealed class SmtpMailSender : IMailSender
  68. {
  69. void IMailSender.Send(string from, string to, string body)
  70. {
  71. System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
  72. mail.From = new System.Net.Mail.MailAddress(from);
  73. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
  74. mail.To.Add(to);
  75. mail.Body = body;
  76. smtp.Send(mail);
  77.  
  78. }
  79. }
  80.  
  81. public class SendingMail
  82. {
  83. public List<string> Errors { get; } = new List<string>();
  84.  
  85. public IEnumerable<Customer> Customers { get; set; }
  86.  
  87. public IEnumerable<Order> Orders { get; set; }
  88.  
  89. public IMailSender Sender { get; set; }
  90.  
  91. public void SendWelcomeEmails()
  92. {
  93. var template = Resources.WelcomeEmailTemplate;
  94. Send(GetNewCustomers(), Resources.WelcomeEmailTemplate);
  95. }
  96.  
  97. public void SendComeBackEmail()
  98. {
  99. var template = Resources.WelcomeEmailTemplate;
  100. var emailContent = String.Format(template);
  101. Send(GetCustomersWithoutRecentOrders(), Resources.ComeBackEmailTemplate);
  102. }
  103.  
  104. private IEnumerable<Customer> GetNewCustomers()
  105. {
  106. var yesterday = DateTime.Now.Date.AddDays(-1);
  107. return Customers.Where(x => x.CreatedDateTime >= yesterday);
  108. }
  109. private IEnumerable<Customer> GetCustomersWithoutRecentOrders()
  110. {
  111. var oneMonthAgo = DateTime.Now.Date.AddMonths(-1);
  112.  
  113. return Customers.Where(c => {
  114. var latestOrder = Orders
  115. .Where(o => o.CustomerEmail == c.Email)
  116. .OrderByDescending(o => o.OrderDatetime)
  117. .FirstOrDefault();
  118.  
  119. return latestOrder != null
  120. && latestOrder.OrderDatetime < oneMonthAgo;
  121. });
  122. }
  123.  
  124. private void Send(IEnumerable<Customer> customers, string body)
  125. {
  126. string titleOfEmail = "Welcome as a new customer at Company!";
  127. string ourEmailAddress = "info@company.com";
  128. int NumberOfRetriesOnError = 2;
  129. int DelayOnError = 10;
  130.  
  131. foreach (var customer in customers)
  132. {
  133. for (int i = 0; i <= NumberOfRetriesOnError; ++i)
  134. {
  135. try
  136. {
  137. Sender.Send(ourEmailAddress, customer.Email, titleOfEmail);
  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); // Include customeremail
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement