Advertisement
Guest User

Untitled

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