У меня есть следующий код, который зацикливается и должен отправлять одно электронное письмо на каждый запрошенный список заказов.Таким образом, он отправляет несколько отдельных электронных писем с заказами для каждого клиента, а не только один список заказов по электронной почте.
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);
}