Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace EmailSender
- {
- public class SendingMail
- {
- private static List<Customer> customers = DataLayer.ListCustomers();
- public static void SendEmail(string recipients, string from, string subject, string body)
- {
- foreach (Customer customer in customers)
- {
- var mailMessage = new MailMessage();
- mailMessage.To.Add(string.Join(",", recipients));
- mailMessage.From = new MailAddress(from);
- mailMessage.Subject = subject;
- mailMessage.Body = body;
- }
- }
- public static bool SendWelcomeMail()
- {
- string welcomeSubject = "Welcome as a new customer at Company!";
- string ourEmailAddress = "info@company.com";
- string bodyTemplate = "Hi {0}<br>We would like to welcome you as customer on our site!<br><br>Best Regards,<br>lcompany Team";
- try
- {
- foreach (var customer in DataLayer.ListCustomers())
- {
- if (customer.CreatedDateTime >= DateTime.Now.AddDays(-1))
- {
- SendEmail(customer.Email, ourEmailAddress, welcomeSubject, string.Format(bodyTemplate, customer.Email));
- #if DEBUG
- // In debug mode, send email to
- Console.WriteLine("Send mail to:" + customer.Email);
- #else
- //Create a SmtpClient to our smtphost: yoursmtphost
- System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
- Send mail
- smtp.Send(mail);
- #endif
- }
- }
- return true;
- //All mails are sent! Success!
- }
- catch (Exception)
- {
- //Something went wrong :(
- return false;
- }
- }
- public static bool SendComeBackEmail(string voucherCode)
- {
- string comeBackSubject = "Welcome as a new customer at Company!";
- string ourEmailAddress = "info@company.com";
- string bodyTemplate = "Hi {0}" +
- "<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." +
- "<br>Voucher: " + voucherCode +
- "<br><br>Best Regards,<br>company Team";
- List<Customer> customers = DataLayer.ListCustomers();
- List<Order> customerOrders = DataLayer.ListOrders();
- try
- {
- foreach (var customer in DataLayer.ListCustomers())
- {
- bool Send = false;
- foreach (Order order in customerOrders)
- if (customer.Email == order.CustomerEmail && order.OrderDatetime <= DateTime.Now.AddMonths(-1))
- {
- //We send email to that customer
- Send = true;
- }
- //Send if customer hasn't put order
- if (Send == true)
- {
- SendEmail(customer.Email, ourEmailAddress, comeBackSubject, string.Format(bodyTemplate, customer.Email));
- #if DEBUG
- Console.WriteLine("Send mail to:" + customer.Email);
- #else
- //Create a SmtpClient to our smtphost: yoursmtphost
- System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
- Send mail
- smtp.Send(m);
- #endif
- }
- }
- //All mails are sent! Success!
- return true;
- }
- catch (Exception)
- {
- //Something went wrong :(
- return false;
- }
- }
- }
- }
- internal class Program
- {
- private static void Main(string[] args)
- {
- Console.WriteLine("Send Welcomemail");
- bool welcomeMailSuccess = SendingMail.SendWelcomeMail();
- #if DEBUG
- //Debug mode, always send Comeback mail
- Console.WriteLine("Send Comebackmail");
- bool comeBackEmailSuccess = SendingMail.SendComeBackEmail("Thisisavouchercode");
- #else
- //Every Sunday run Comeback mail
- if (DateTime.Now.DayOfWeek.Equals(DayOfWeek.Monday))
- {
- Console.WriteLine("Send Comebackmail");
- comebackEmailSuccess = SendComeBackEmail("Thisisavouchercode");
- }
- #endif
- //Check if the sending went OK
- if (comeBackEmailSuccess == true)
- {
- Console.WriteLine("All mails are sent, I hope...");
- }
- //Check if the sending was not going well...
- if (comeBackEmailSuccess == false)
- {
- Console.WriteLine("Oops, something went wrong when sending mail (I think...)");
- }
- Console.ReadKey();
- }
- }
- 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