Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace EmailSenderProgram
  5. {
  6. internal class Program
  7. {
  8. /// <summary>
  9. /// This application is run everyday
  10. /// </summary>
  11. /// <param name="args"></param>
  12. private static void Main(string[] args)
  13. {
  14. Console.WriteLine("Send Welcomemail");
  15. bool success = DoEmailWork();
  16.  
  17. #if DEBUG
  18.  
  19. Console.WriteLine("Send Comebackmail");
  20. success = DoEmailWork2("CompanyComebackToUs");
  21. #else
  22.  
  23. if (DateTime.Now.DayOfWeek.Equals(DayOfWeek.Monday))
  24. {
  25. Console.WriteLine("Send Comebackmail");
  26. success = DoEmailWork2("CompanyComeBackToUs");
  27. }
  28. #endif
  29.  
  30. if (success == true)
  31. {
  32. Console.WriteLine("All mails are sent, I hope...");
  33. }
  34.  
  35. if (success == false)
  36. {
  37. Console.WriteLine("Oops, something went wrong when sending mail (I think...)");
  38. }
  39. Console.ReadKey();
  40. }
  41.  
  42. /// <summary>
  43. /// Send Welcome mail
  44. /// </summary>
  45. /// <returns></returns>
  46. public static bool DoEmailWork()
  47. {
  48. try
  49. {
  50.  
  51. List<Customer> e = DataLayer.ListCustomers();
  52.  
  53.  
  54. for (int i = 0; i < e.Count; i++)
  55. {
  56.  
  57. if (e[i].CreatedDateTime > DateTime.Now.AddDays(-1))
  58. {
  59.  
  60.  
  61. System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
  62.  
  63. m.To.Add(e[i].Email);
  64. Add subject
  65. m.Subject = "Welcome as a new customer at Company!";
  66. Send mail from company@info.com
  67. m.From = new System.Net.Mail.MailAddress("compay@info.com);
  68.  
  69. m.Body = "Hi " + e[i].Email +
  70. "<br>We would like to welcome you as customer on our site!<br><br>Best Regards,<br>Company Team";
  71. #if DEBUG
  72.  
  73. Console.WriteLine("Send mail to:" + e[i].Email);
  74. #else
  75. Create a SmtpClient to our smtphost: yoursmtphost
  76. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
  77. smtp.Send(m);
  78. #endif
  79. }
  80. }
  81. return true;
  82. }
  83. catch (Exception)
  84. {
  85. return false;
  86. }
  87. }
  88.  
  89. /// <summary>
  90. /// Send Customer ComebackMail
  91. /// </summary>
  92. /// <param name="v"></param>
  93. /// <returns></returns>
  94. private static bool DoEmailWork2(string v)
  95. {
  96. try
  97. {
  98. //List all customers
  99. List<Customer> e = DataLayer.ListCustomers();
  100. //List all orders
  101. List<Order> f = DataLayer.ListOrders();
  102.  
  103. //loop through list of customers
  104. foreach (Customer c in e)
  105. {
  106. // We send mail if customer hasn't put an order
  107. bool Send = true;
  108. //loop through list of orders to see if customer don't exist in that list
  109. foreach (Order o in f)
  110. {
  111. // Email exists in order list
  112. if (c.Email == o.CustomerEmail)
  113. {
  114. //We don't send email to that customer
  115. Send = false;
  116. }
  117. }
  118.  
  119. //Send if customer hasn't put order
  120. if (Send == true)
  121. {
  122. //Create a new MailMessage
  123. System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
  124. //Add customer to reciever list
  125. m.To.Add(c.Email);
  126. //Add subject
  127. m.Subject = "We miss you as a customer";
  128. m.From = new System.Net.Mail.MailAddress("company@info.com");
  129. //Add body to mail
  130. m.Body = "Hi " + c.Email +
  131. "<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." +
  132. "<br>Voucher: " + v +
  133. "<br><br>Best Regards,<br>Company Team";
  134. #if DEBUG
  135. //Don't send mails in debug mode, just write the emails in console
  136. Console.WriteLine("Send mail to:" + c.Email);
  137. #else
  138. Create a SmtpClient to our smtphost: yoursmtphost
  139. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
  140.  
  141. smtp.Send(m);
  142. #endif
  143. }
  144. }
  145. return true;
  146. }
  147. catch (Exception)
  148. {
  149. return false;
  150. }
  151. }
  152. }
  153. }
  154.  
  155. namespace EmailSenderProgram
  156. {
  157. public class Customer
  158. {
  159. public string Email { get; set; }
  160. public DateTime CreatedDateTime { get; set; }
  161. }
  162.  
  163. public class Order
  164. {
  165. public string CustomerEmail { get; set; }
  166. public DateTime OrderDatetime { get; set; }
  167. }
  168.  
  169. class DataLayer
  170. {
  171. /// <summary>
  172. /// Mockup method for all customers
  173. /// </summary>
  174. public static List<Customer> ListCustomers()
  175. {
  176. return new List<Customer>()
  177. {
  178. new Customer(){Email = "mail1@mail.com", CreatedDateTime = DateTime.Now.AddHours(-7)},
  179. new Customer(){Email = "mail2@mail.com", CreatedDateTime = DateTime.Now.AddDays(-1)},
  180. new Customer(){Email = "mail3@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-6)},
  181. new Customer(){Email = "mail4@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-1)},
  182. new Customer(){Email = "mail5@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-2)},
  183. new Customer(){Email = "mail6@mail.com", CreatedDateTime = DateTime.Now.AddDays(-5)}
  184. };
  185. }
  186.  
  187. /// <summary>
  188. /// Mockup method for listing all orders
  189. /// </summary>
  190. public static List<Order> ListOrders()
  191. {
  192. return new List<Order>()
  193. {
  194. new Order(){CustomerEmail = "mail3@mail.com", OrderDatetime = DateTime.Now.AddMonths(-6)},
  195. new Order(){CustomerEmail = "mail5@mail.com", OrderDatetime = DateTime.Now.AddMonths(-2)},
  196. new Order(){CustomerEmail = "mail6@mail.com", OrderDatetime = DateTime.Now.AddDays(-2)}
  197. };
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement