简体   繁体   English

每个客户在循环上发送 email ASP.NET MVC

[英]Sending email on the loop Per Customer ASP.NET MVC

I have the following code that loops around and is supposed to send a single email per requested order list.我有以下循环的代码,应该为每个请求的订单列表发送一个 email。 So it sends multiple separated order emails for each customer rather than just the one email order list.因此,它为每个客户发送多封单独的订单电子邮件,而不仅仅是一个 email 订单列表。

public ActionResult SaveOrder(string name, Order[] order)
{
    string result = "Error! Order Is Not Complete!";
    if (name != null && address != null && email != null  && order != null)
    {
        var cutomerId = Guid.NewGuid();
        Customer model = new Customer();
        model.CustomerId = cutomerId;
        model.Name = name;
        model.Address = address;
        model.OrderDate = DateTime.Now;
        model.Email = email;
        db.Customers.Add(model);


        foreach (var item in order)
        {
            var orderId = Guid.NewGuid();
            Order O = new Order();
            O.OrderId = orderId;
            O.ProductName = item.ProductName;
            O.Quantity = item.Quantity;
            O.Price = item.Price;
            O.Amount = item.Amount;
            O.CustomerId = cutomerId;
            db.Orders.Add(O);

            var customername = model.Name;

            if (model.Name != null)
            {
                //Send Email to User
                SendVerificationLinkEmail(model.Email, model.Name, O.ProductName);
            }
        }
            db.SaveChanges()                

        result = "Success! Order Is Complete!";
    }
    return Json(result, JsonRequestBehavior.AllowGet);
}

[NonAction]
public void SendVerificationLinkEmail(string email, string name, string productname)
{


    var fromEmail = new MailAddress("test@test.com", "Laundry");
    var toEmail = new MailAddress(email);
    var fromEmailPassword = "xxxxx"; // Replace with actual password
    string subject = "Your " + productname + " order request has been scheduled ";

    string body = "";

    using (StreamReader reader = new StreamReader(Server.MapPath("~/HtmlPage1.html")))
    {
        body = reader.ReadToEnd();
    }

    body = body.Replace("{name}", name);
    body = body.Replace("{emailID}", email);
    body = body.Replace("{productname}", productname);



    var smtp = new SmtpClient
    {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromEmail.Address, fromEmailPassword)

    };

    using (var message = new MailMessage(fromEmail, toEmail)
    {
        Subject = subject,
        Body = body,
        IsBodyHtml = true

    })
        smtp.Send(message);
}

Changing this:改变这个:

        foreach (var item in order)
        {
            var orderId = Guid.NewGuid();

            ... // code removed for clarity

            if (model.Name != null)
            {
                //Send Email to User
                SendVerificationLinkEmail(model.Email, model.Name, O.ProductName);
            }
        }

to this: (take the if statement out of the foreach loop)为此:(将 if 语句从 foreach 循环中取出)

        foreach (var item in order)
        {
            var orderId = Guid.NewGuid();

            ... // code removed for clarity

        }
        if (model.Name != null)
        {
            //Send Email to User
            SendVerificationLinkEmail(model.Email, model.Name, O.ProductName);
        }

will send one email per customer order, instead of one email per order item.将为每个客户订单发送一个 email,而不是每个订单项目发送一个 email。 (Although you're passing o.ProductName to the SendVerificationLinkEmail method, so the email will presumably only contain the last ProductName since that appears to be different for each line item.) (虽然您将o.ProductName传递给SendVerificationLinkEmail方法,但 email 可能只包含最后一个 ProductName,因为每个行项目似乎都不同。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM