Exemplo n.º 1
0
        public ActionResult Checkout(Cart cart, ShippingDetails details)
        {
            if (cart.Lines.Count() <= 0)
            {
                ModelState.AddModelError("NoItem", "Sorry, your cart is empty!");
            }

            if (ModelState.IsValid)
            {
                orderProcessor.ProcessOrder(cart, details);
                cart.Clear();
                return View("Completed");
            }

            else
            {
                return View(details);
            }
        }
        public void ProcessOrder(Cart cart, ShippingDetails shippingInfo)
        {
            using (SmtpClient client = new SmtpClient())
            {
                client.EnableSsl = emailSetting.UseSSL;
                client.Host = emailSetting.ServerName;
                client.Port = emailSetting.Port;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(emailSetting.UserName, emailSetting.Password);
                if (emailSetting.WriteAsFile)
                {
                    client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    client.PickupDirectoryLocation = emailSetting.FileLocation;
                    client.EnableSsl = false;
                }

                StringBuilder mailBody = new System.Text.StringBuilder();
                mailBody.AppendLine("A new order has been submit")
                .AppendLine("--------------------------")
                .AppendLine("Lines:");

                foreach (var line in cart.Lines)
                {
                    var subtotal = line.Product.Price * line.Quantity;
                    mailBody.AppendFormat("{0:c} x {1}  (subtotal:{2:c}", line.Product.Price, line.Quantity, subtotal);
                }

                MailMessage message = new MailMessage(emailSetting.MailFromAddress, emailSetting.MailToAddress, "New submit order", mailBody.ToString());

                if (emailSetting.WriteAsFile)
                {
                    message.BodyEncoding = Encoding.UTF8;
                }

                client.Send(message);
            }
        }