public void ProcessOrder(Cart cart, ShippingDetails shippingDetails)
        {
            using (SmtpClient smptClient = new SmtpClient())
            {
                smptClient.EnableSsl = emailSettings.UseSSL;
                smptClient.Host = emailSettings.ServerName;
                smptClient.Port = emailSettings.ServerPort;
                smptClient.UseDefaultCredentials = false;

                smptClient.Credentials = new NetworkCredential(emailSettings.UserName, emailSettings.Password);

                if (emailSettings.WriteAsFile)
                {
                    smptClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smptClient.PickupDirectoryLocation = emailSettings.FileLocation;
                    smptClient.EnableSsl = false;
                }
                StringBuilder body = new StringBuilder();

                body
                    .AppendLine("Размещен новый заказ")
                    .AppendLine("---")
                    .AppendLine("Товары:");
                foreach (CartLine line in cart.Lines)
                {
                    body.AppendFormat("{0} x{1} (Subtotal {2})",line.Product.Name,line.Quantity,line.Product.Price*line.Quantity);
                }

                body.AppendFormat("Итого: {0}", cart.ComputeTotalValue())
                    .AppendLine("---")
                    .AppendLine("Доставка")
                    .AppendLine(shippingDetails.Name)
                    .AppendLine(shippingDetails.Line1)
                    .AppendLine(shippingDetails.Line2 ?? "")
                    .AppendLine(shippingDetails.Line3 ?? "")
                    .AppendLine(shippingDetails.State ?? "")
                    .AppendLine(shippingDetails.City)
                    .AppendLine(shippingDetails.Zip)
                    .AppendLine(shippingDetails.Country)
                    .AppendLine(shippingDetails.Line1)
                    .AppendLine("---")
                    .AppendFormat("GiftWrap: {0}", shippingDetails.GiftWrap ? "Yes" : "No");

                MailMessage mailMessage = new MailMessage(
                    emailSettings.MailFromAdress,
                    emailSettings.MailToAdress,
                    "New Order",
                    body.ToString()
                    );
                if (emailSettings.WriteAsFile)
                {
                    mailMessage.BodyEncoding = Encoding.GetEncoding("iso-8859-1");
                }

                smptClient.Send(mailMessage);

            }
        }
Exemple #2
0
        public void Cart_Can_Compute_Sum()
        {
            var pruductsArray = products.Products.ToArray();
            Cart target = new Cart();
            target.AddItem(pruductsArray[0], 1);
            target.AddItem(pruductsArray[1], 2);
            var resultValue = target.ComputeTotalValue();
            var resultCount = target.ComputeTotalCount();

            var checkresult = pruductsArray[0].Price + pruductsArray[1].Price * 2;
            Assert.AreEqual(resultValue, checkresult);
            Assert.AreEqual(resultCount, 3);
        }