Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void Main(string[] args)
- {
- Sender.Errors.Clear();
- Sender.SendWelcomeEmails();
- SendingMail.SendComeBackEmail("Thisisavouchercode");
- if (sender.Errors.Any())
- Console.WriteLine("All mails are sent, I hope...");
- }
- public class Customer
- {
- public string Email { get; set; }
- public DateTime CreatedDateTime { get; set; }
- }
- public class Order
- {
- public string CustomerEmail { get; set; }
- public DateTime OrderDatetime { get; set; }
- }
- class DataLayer
- {
- /// <summary>
- /// Mockup method for all customers
- /// </summary>
- public static List<Customer> ListCustomers()
- {
- return new List<Customer>()
- {
- new Customer(){Email = "mail1@mail.com", CreatedDateTime = DateTime.Now.AddHours(-7)},
- new Customer(){Email = "mail2@mail.com", CreatedDateTime = DateTime.Now.AddDays(-1)},
- new Customer(){Email = "mail3@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-6)},
- new Customer(){Email = "mail4@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-1)},
- new Customer(){Email = "mail5@mail.com", CreatedDateTime = DateTime.Now.AddMonths(-2)},
- new Customer(){Email = "mail6@mail.com", CreatedDateTime = DateTime.Now.AddDays(-5)}
- };
- }
- /// <summary>
- /// Mockup method for listing all orders
- /// </summary>
- public static List<Order> ListOrders()
- {
- return new List<Order>()
- {
- new Order(){CustomerEmail = "mail3@mail.com", OrderDatetime = DateTime.Now.AddMonths(-6)},
- new Order(){CustomerEmail = "mail5@mail.com", OrderDatetime = DateTime.Now.AddMonths(-2)},
- new Order(){CustomerEmail = "mail6@mail.com", OrderDatetime = DateTime.Now.AddDays(-2)}
- };
- }
- }
- public interface IMailSender
- {
- void Send(string from, string to, string body);
- }
- sealed class NullMailSender : IMailSender
- {
- void IMailSender.Send(string from, string to, string body)
- {
- }
- }
- sealed class SmtpMailSender : IMailSender
- {
- void IMailSender.Send(string from, string to, string body)
- {
- System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
- mail.From = new System.Net.Mail.MailAddress(from);
- System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
- mail.To.Add(to);
- mail.Body = body;
- smtp.Send(mail);
- }
- }
- public class SendingMail
- {
- public List<string> Errors { get; } = new List<string>();
- public IEnumerable<Customer> Customers { get; set; }
- public IEnumerable<Order> Orders { get; set; }
- public IMailSender Sender { get; set; }
- public void SendWelcomeEmails()
- {
- var template = Resources.WelcomeEmailTemplate;
- Send(GetNewCustomers(), Resources.WelcomeEmailTemplate);
- }
- public void SendComeBackEmail()
- {
- var template = Resources.WelcomeEmailTemplate;
- var emailContent = String.Format(template);
- Send(GetCustomersWithoutRecentOrders(), Resources.ComeBackEmailTemplate);
- }
- private IEnumerable<Customer> GetNewCustomers()
- {
- var yesterday = DateTime.Now.Date.AddDays(-1);
- return Customers.Where(x => x.CreatedDateTime >= yesterday);
- }
- private IEnumerable<Customer> GetCustomersWithoutRecentOrders()
- {
- var oneMonthAgo = DateTime.Now.Date.AddMonths(-1);
- return Customers.Where(c => {
- var latestOrder = Orders
- .Where(o => o.CustomerEmail == c.Email)
- .OrderByDescending(o => o.OrderDatetime)
- .FirstOrDefault();
- return latestOrder != null
- && latestOrder.OrderDatetime < oneMonthAgo;
- });
- }
- private void Send(IEnumerable<Customer> customers, string body)
- {
- foreach (Customer customer in customers)
- Send(customer, body);
- }
- private void Send(Customer customer, string body)
- {
- MailMessage message = GenerateMessage(customer, body);
- int NumberOfRetriesOnError = 2;
- int DelayOnError = 10;
- for (int i = 0; i <= NumberOfRetriesOnError; ++i)
- {
- try
- {
- Sender.Send(GenerateMessage);
- return;
- }
- catch (SmtpException e)
- {
- if (i < NumberOfRetriesOnError)
- Thread.Sleep((i + 1) * DelayOnError);
- else
- Errors.Add(e.Message + customer.Email); // Include customeremail
- }
- }
- }
- private MailMessage GenerateMessage(Customer customer, string body)
- {
- string subject = "...";
- string from = "...";
- string to = customer.Email;
- MailMessage retVal = new MailMessage(from, to, subject, body);
- return retVal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement