public void ProcessOrder(Cart cart, ShippingDetails shippingInfo) { using (var mail = new SmtpClient()) { mail.EnableSsl = emailSettings.UseSsl; mail.Host = emailSettings.ServerName; mail.Port = emailSettings.ServerPort; mail.UseDefaultCredentials = false; mail.Credentials = new NetworkCredential(emailSettings.Username, emailSettings.Password); if (emailSettings.WriteAsFile) { mail.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; mail.PickupDirectoryLocation = emailSettings.FileLocation; mail.EnableSsl = false; } var body = new StringBuilder() .AppendLine("Новый заказ обработан") .AppendLine("---") .AppendLine("Товары:"); foreach (var line in cart.Lines) { var subtotal = line.Cake.Price * line.Quantity; body.AppendFormat("{0} x {1} [итого: {2:c}]", line.Quantity, line.Cake.Name, subtotal); } body.AppendFormat("Общая стоимость: {0:c}", cart.ComputeTotalValue()) .AppendLine("---") .AppendLine("Доставка:") .AppendLine(shippingInfo.Name) .AppendLine(shippingInfo.Address) .AppendLine(shippingInfo.City) .AppendLine(shippingInfo.Country) .AppendLine("---") .AppendFormat("Подарочная упаковка: {0}", shippingInfo.GiftWrap ? "Да" : "Нет"); var mailMessage = new MailMessage( emailSettings.MailFromAddress, // От кого emailSettings.MailToAddress, // Кому "Новый заказ отправлен!", // Тема body.ToString()); // Тело письма if (emailSettings.WriteAsFile) { mailMessage.BodyEncoding = Encoding.UTF8; } mail.Send(mailMessage); } }
public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails) { if (cart.Lines.Count() == 0) { ModelState.AddModelError("", "Cart is empty!"); } if (ModelState.IsValid) { orderProcessor.ProcessOrder(cart, shippingDetails); cart.Clear(); return View("Completed"); } else { return View(shippingDetails); } }