This is a fictional email sending program for a e-store I've done for practice purposes. EmailSenderProgram is a program sending emails to customers. Currently it sends two types of email: "welcome" and "please come back" email. It's supposed to run daily and write a debug log each day if it worked or not. Posted earlier, updated and now back for more. Revision: 2
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 = "[email protected]";
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 = "[email protected]";
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;
}
}
}
}
Main class program
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();
}
}
Lastly a datalayer class
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 = "[email protected]", CreatedDateTime = DateTime.Now.AddHours(-7)},
new Customer(){Email = "[email protected]", CreatedDateTime = DateTime.Now.AddDays(-1)},
new Customer(){Email = "[email protected]", CreatedDateTime = DateTime.Now.AddMonths(-6)},
new Customer(){Email = "[email protected]", CreatedDateTime = DateTime.Now.AddMonths(-1)},
new Customer(){Email = "[email protected]", CreatedDateTime = DateTime.Now.AddMonths(-2)},
new Customer(){Email = "[email protected]", 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 = "[email protected]", OrderDatetime = DateTime.Now.AddMonths(-6)},
new Order(){CustomerEmail = "[email protected]", OrderDatetime = DateTime.Now.AddMonths(-2)},
new Order(){CustomerEmail = "[email protected]", OrderDatetime = DateTime.Now.AddDays(-2)}
};
}
This question is a follow-up to: E-store automatic email send, to be run daily