Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class Program
- {
- private static void Main(string[] args)
- {
- Sender.Errors.Clear();
- Sender.SendComeBackEmail("Thisisavouchercode");
- if (Sender.Errors.Any())
- Console.WriteLine("All mails are sent, I hope...");
- namespace EmailSender
- {
- sealed 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)
- {
- string titleOfEmail = "Welcome as a new customer at Company!";
- string ourEmailAddress = "info@company.com";
- int NumberOfRetriesOnError = 2;
- int DelayOnError = 10;
- foreach (var customer in customers)
- {
- for (int i = 0; i <= NumberOfRetriesOnError; ++i)
- {
- try
- {
- Sender.Send(ourEmailAddress, customer.Email, titleOfEmail);
- return;
- }
- catch (SmtpException e)
- {
- if (i < NumberOfRetriesOnError)
- Thread.Sleep((i + 1) * DelayOnError);
- else
- Errors.Add(e.Message + customer); // Include customeremail
- }
- }
- }
- }
- }
- }
- namespace EmailSender
- {
- 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);
- }
- }
- }
- namespace EmailSender
- {
- 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)}
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement