Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. namespace EmailSender
  2. {
  3. public class SendingMail
  4. {
  5. private static List<Customer> customers = DataLayer.ListCustomers();
  6. public static void SendEmail(string recipients, string from, string subject, string body)
  7. {
  8.  
  9. foreach (Customer customer in customers)
  10. {
  11. var mailMessage = new MailMessage();
  12. mailMessage.To.Add(string.Join(",", recipients));
  13. mailMessage.From = new MailAddress(from);
  14. mailMessage.Subject = subject;
  15. mailMessage.Body = body;
  16. }
  17. }
  18.  
  19. public static bool SendWelcomeMail()
  20. {
  21. string welcomeSubject = "Welcome as a new customer at Company!";
  22. string ourEmailAddress = "info@company.com";
  23. string bodyTemplate = "Hi {0}<br>We would like to welcome you as customer on our site!<br><br>Best Regards,<br>lcompany Team";
  24.  
  25. try
  26. {
  27. foreach (var customer in DataLayer.ListCustomers())
  28. {
  29. if (customer.CreatedDateTime >= DateTime.Now.AddDays(-1))
  30. {
  31. SendEmail(customer.Email, ourEmailAddress, welcomeSubject, string.Format(bodyTemplate, customer.Email));
  32.  
  33. #if DEBUG
  34. // In debug mode, send email to
  35. Console.WriteLine("Send mail to:" + customer.Email);
  36. #else
  37. //Create a SmtpClient to our smtphost: yoursmtphost
  38. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
  39. Send mail
  40. smtp.Send(mail);
  41. #endif
  42. }
  43. }
  44. return true;
  45. //All mails are sent! Success!
  46. }
  47. catch (Exception)
  48. {
  49. //Something went wrong :(
  50. return false;
  51. }
  52. }
  53.  
  54. public static bool SendComeBackEmail(string voucherCode)
  55. {
  56. string comeBackSubject = "Welcome as a new customer at Company!";
  57. string ourEmailAddress = "info@company.com";
  58. string bodyTemplate = "Hi {0}" +
  59. "<br>We miss you as a customer. Our shop is filled with nice products. Here is a voucher that gives you 50 kr to shop for." +
  60. "<br>Voucher: " + voucherCode +
  61. "<br><br>Best Regards,<br>company Team";
  62.  
  63. List<Customer> customers = DataLayer.ListCustomers();
  64. List<Order> customerOrders = DataLayer.ListOrders();
  65.  
  66. try
  67. {
  68. foreach (var customer in DataLayer.ListCustomers())
  69. {
  70. bool Send = false;
  71. foreach (Order order in customerOrders)
  72. if (customer.Email == order.CustomerEmail && order.OrderDatetime <= DateTime.Now.AddMonths(-1))
  73. {
  74. //We send email to that customer
  75. Send = true;
  76. }
  77.  
  78. //Send if customer hasn't put order
  79. if (Send == true)
  80. {
  81. SendEmail(customer.Email, ourEmailAddress, comeBackSubject, string.Format(bodyTemplate, customer.Email));
  82. #if DEBUG
  83.  
  84. Console.WriteLine("Send mail to:" + customer.Email);
  85. #else
  86. //Create a SmtpClient to our smtphost: yoursmtphost
  87. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
  88. Send mail
  89. smtp.Send(m);
  90. #endif
  91. }
  92. }
  93. //All mails are sent! Success!
  94. return true;
  95. }
  96. catch (Exception)
  97. {
  98. //Something went wrong :(
  99. return false;
  100. }
  101. }
  102. }
  103. }
  104.  
  105. internal class Program
  106. {
  107. private static void Main(string[] args)
  108. {
  109. Console.WriteLine("Send Welcomemail");
  110. bool welcomeMailSuccess = SendingMail.SendWelcomeMail();
  111. #if DEBUG
  112. //Debug mode, always send Comeback mail
  113. Console.WriteLine("Send Comebackmail");
  114. bool comeBackEmailSuccess = SendingMail.SendComeBackEmail("Thisisavouchercode");
  115. #else
  116. //Every Sunday run Comeback mail
  117. if (DateTime.Now.DayOfWeek.Equals(DayOfWeek.Monday))
  118. {
  119. Console.WriteLine("Send Comebackmail");
  120. comebackEmailSuccess = SendComeBackEmail("Thisisavouchercode");
  121. }
  122. #endif
  123.  
  124. //Check if the sending went OK
  125. if (comeBackEmailSuccess == true)
  126. {
  127. Console.WriteLine("All mails are sent, I hope...");
  128. }
  129. //Check if the sending was not going well...
  130. if (comeBackEmailSuccess == false)
  131. {
  132. Console.WriteLine("Oops, something went wrong when sending mail (I think...)");
  133. }
  134. Console.ReadKey();
  135. }
  136. }
  137.  
  138. public class Customer
  139. {
  140. public string Email { get; set; }
  141. public DateTime CreatedDateTime { get; set; }
  142. }
  143.  
  144. public class Order
  145. {
  146. public string CustomerEmail { get; set; }
  147. public DateTime OrderDatetime { get; set; }
  148. }
  149.  
  150. class DataLayer
  151. {
  152. /// <summary>
  153. /// Mockup method for all customers
  154. /// </summary>
  155. public static List<Customer> ListCustomers()
  156. {
  157. return new List<Customer>()
  158. {
  159. new Customer(){Email = "mail1@mail.com", CreatedDateTime = DateTime.Now.AddHours(-7)},
  160. new Customer(){Email = "mail2@mail.com", CreatedDateTime = DateTime.Now.AddDays(-1)},
  161. new Customer(){Email = "mail3@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-6)},
  162. new Customer(){Email = "mail4@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-1)},
  163. new Customer(){Email = "mail5@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-2)},
  164. new Customer(){Email = "mail6@mail.com", CreatedDateTime = DateTime.Now.AddDays(-5)}
  165. };
  166. }
  167.  
  168. /// <summary>
  169. /// Mockup method for listing all orders
  170. /// </summary>
  171. public static List<Order> ListOrders()
  172. {
  173. return new List<Order>()
  174. {
  175. new Order(){CustomerEmail = "mail3@mail.com", OrderDatetime = DateTime.Now.AddMonths(-6)},
  176. new Order(){CustomerEmail = "mail5@mail.com", OrderDatetime = DateTime.Now.AddMonths(-2)},
  177. new Order(){CustomerEmail = "mail6@mail.com", OrderDatetime = DateTime.Now.AddDays(-2)}
  178. };
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement