从列表创建函数

时间:2017-08-10 07:34:36

标签: email

这是一个虚构的消息发送程序,我已经为实践目的而做了。 MessageSender`是一个向客户发送电子邮件的程序。目前它发送两种类型的电子邮件:" welcome"和"请回来"电子邮件。它应该每天运行并且每天写一个调试日志,如果它工作与否。

如何在SendWelcomeMail中创建代码功能并调用它,我尝试自己制作一个,但我不确定如何调用该函数并将收件人功能与数据层中的客户电子邮件相关联。

namespace EmailSender
{
    internal class Program
    {

        private static void Main(string[] args)
        {
            //Call the method that do the work for me, I.E. sending the mails
            Console.WriteLine("Send Welcomemail");
            bool welcomeEmailSucess = SendWelcomeMail();
#if DEBUG
            //Debug mode, always send Comeback mail
            Console.WriteLine("Send Comebackmail");
            bool comeBackEmailSuccess = SendComeBackEmail("ComebackToUs");
#else
            //Every Sunday run Comeback mail
            if (DateTime.Now.DayOfWeek.Equals(DayOfWeek.Monday))
            {
                Console.WriteLine("Send Comebackmail");
                comeBackEmailSuccess = SendComeBackEmail("ComebackToUs");
            }
#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 static bool SendWelcomeMail()
        {
            try
            {
                //List all customers
                List<Customer> customers = DataLayer.ListCustomers();

                //loop through list of new customers
                foreach (Customer c in customers)
                {
                    //If the customer is newly registered, one day back in time
                    if (c.CreatedDateTime >= DateTime.Now.AddDays(-1))
                    {
                        System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
                        //Add subject
                        m.Subject = "Welcome as a new customer at Company!";
                        //Send mail from info@company.com
                        m.From = new System.Net.Mail.MailAddress("info@company.com");
                        //Add body to mail
                        m.Body = "Hi " + c.Email +
                                 "<br>We would like to welcome you as customer on our site!<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");
                    //Send mail
                    smtp.Send(m);
#endif
                    }
                }
                //All mails are sent! Success!
                return true;
            }
            catch (Exception)
            {
                //Something went wrong :(
                return false;
            }
        }

数据层

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
{
    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)}
                       };
        }
        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)}
                       };
        }
    }
}

1 个答案:

答案 0 :(得分:1)

现在您没有使用SendEmail功能中的收件人列表 - 您忽略它并循环遍历DataLayer返回的所有客户,因此请更改它。

如果您想继续做您正在做的事情,例如,如果该客户应该收到该电子邮件,请循环访问每个客户并直接发送电子邮件,然后将该方法更改为以下内容:

public static void SendEmail(string toAddress, string from, string subject, 
string body)
    {
        //Create a new MailMessage
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

        //Add customer to reciever list
        mail.To.Add(toAddress);

        //Add subject
        mail.Subject = subject;

        //Send mail from company@info.com
        mail.From = new System.Net.Mail.MailAddress(from);

        //Add body to mail
        mail.Body = body;

       // Send it or write to console depending on debug
    }

并调用它而不是在SendWelcomeMail和SendComeBackEmail这两个函数中创建电子邮件。 而不是

//loop through list of new customers
foreach (Customer c in customers)
{
   if (c.CreatedDateTime >= DateTime.Now.AddDays(-1))
   {
        System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
        //Add subject
        m.Subject = "Welcome as a new customer at Company!";
        //Send mail from info@company.com
        m.From = new System.Net.Mail.MailAddress("info@company.com");
        //Add body to mail
        m.Body = "Hi " + c.Email +
        "<br>We would like to welcome you as customer on our site!<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);
      ...

使用类似

的内容
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>Company Team";

   //loop through list of new customers
   foreach (Customer c in customers)
   {
       if (c.CreatedDateTime >= DateTime.Now.AddDays(-1))
       {
           SendEmail(c.Email, ourEmailAddress, welcomeSubject, string.Format(bodyTemplate, c.Email));

SendComebackEmail中的类似内容。

这是使用该功能的最简单方法,而不改变代码的结构,我现在可以想到。 如果您想要一个收件人列表(例如您发布的版本),您将不得不进行大量更改,因为您在邮件正文中有客户电子邮件,因此您不能拥有多个收件人收件人,只有一个身体。

希望这有帮助。