Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace EmailSenderProgram
- {
- internal class Program
- {
- /// <summary>
- /// This application is run everyday
- /// </summary>
- /// <param name="args"></param>
- private static void Main(string[] args)
- {
- Console.WriteLine("Send Welcomemail");
- bool success = DoEmailWork();
- #if DEBUG
- Console.WriteLine("Send Comebackmail");
- success = DoEmailWork2("CompanyComebackToUs");
- #else
- if (DateTime.Now.DayOfWeek.Equals(DayOfWeek.Monday))
- {
- Console.WriteLine("Send Comebackmail");
- success = DoEmailWork2("CompanyComeBackToUs");
- }
- #endif
- if (success == true)
- {
- Console.WriteLine("All mails are sent, I hope...");
- }
- if (success == false)
- {
- Console.WriteLine("Oops, something went wrong when sending mail (I think...)");
- }
- Console.ReadKey();
- }
- /// <summary>
- /// Send Welcome mail
- /// </summary>
- /// <returns></returns>
- public static bool DoEmailWork()
- {
- try
- {
- List<Customer> e = DataLayer.ListCustomers();
- for (int i = 0; i < e.Count; i++)
- {
- if (e[i].CreatedDateTime > DateTime.Now.AddDays(-1))
- {
- System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
- m.To.Add(e[i].Email);
- Add subject
- m.Subject = "Welcome as a new customer at Company!";
- Send mail from company@info.com
- m.From = new System.Net.Mail.MailAddress("compay@info.com);
- m.Body = "Hi " + e[i].Email +
- "<br>We would like to welcome you as customer on our site!<br><br>Best Regards,<br>Company Team";
- #if DEBUG
- Console.WriteLine("Send mail to:" + e[i].Email);
- #else
- Create a SmtpClient to our smtphost: yoursmtphost
- System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
- smtp.Send(m);
- #endif
- }
- }
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- /// <summary>
- /// Send Customer ComebackMail
- /// </summary>
- /// <param name="v"></param>
- /// <returns></returns>
- private static bool DoEmailWork2(string v)
- {
- try
- {
- //List all customers
- List<Customer> e = DataLayer.ListCustomers();
- //List all orders
- List<Order> f = DataLayer.ListOrders();
- //loop through list of customers
- foreach (Customer c in e)
- {
- // We send mail if customer hasn't put an order
- bool Send = true;
- //loop through list of orders to see if customer don't exist in that list
- foreach (Order o in f)
- {
- // Email exists in order list
- if (c.Email == o.CustomerEmail)
- {
- //We don't send email to that customer
- Send = false;
- }
- }
- //Send if customer hasn't put order
- if (Send == true)
- {
- //Create a new MailMessage
- System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
- //Add customer to reciever list
- m.To.Add(c.Email);
- //Add subject
- m.Subject = "We miss you as a customer";
- m.From = new System.Net.Mail.MailAddress("company@info.com");
- //Add body to mail
- m.Body = "Hi " + c.Email +
- "<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: " + v +
- "<br><br>Best Regards,<br>Company Team";
- #if DEBUG
- //Don't send mails in debug mode, just write the emails in console
- Console.WriteLine("Send mail to:" + c.Email);
- #else
- Create a SmtpClient to our smtphost: yoursmtphost
- System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
- smtp.Send(m);
- #endif
- }
- }
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- }
- }
- namespace EmailSenderProgram
- {
- 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