From 27f0d261686d633c6ee2faea4af3180caf6ba520 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Wed, 22 Sep 2021 14:51:34 +1000 Subject: [PATCH] 9.3.3.2 Implementing the Interface; Listing 9-15 The Contents of the EmailOrderProcessor.cs File --- .../Abstract/IOrderProcessor.cs | 2 +- .../Concrete/EmailOrderProcessor.cs | 93 +++++++++++++++++++ .../SportsStore.Domain.csproj | 1 + 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 SportsStore/SportsStore.Domain/Concrete/EmailOrderProcessor.cs diff --git a/SportsStore/SportsStore.Domain/Abstract/IOrderProcessor.cs b/SportsStore/SportsStore.Domain/Abstract/IOrderProcessor.cs index 3ccbe13..dd477cf 100644 --- a/SportsStore/SportsStore.Domain/Abstract/IOrderProcessor.cs +++ b/SportsStore/SportsStore.Domain/Abstract/IOrderProcessor.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace SportsStore.Domain.Abstract { - interface IOrderProcessor + public interface IOrderProcessor { void ProcessOrder(Cart cart, ShippingDetails shippingDetails); } diff --git a/SportsStore/SportsStore.Domain/Concrete/EmailOrderProcessor.cs b/SportsStore/SportsStore.Domain/Concrete/EmailOrderProcessor.cs new file mode 100644 index 0000000..77f4b9e --- /dev/null +++ b/SportsStore/SportsStore.Domain/Concrete/EmailOrderProcessor.cs @@ -0,0 +1,93 @@ +using SportsStore.Domain.Abstract; +using SportsStore.Domain.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Mail; +using System.Text; +using System.Threading.Tasks; + +namespace SportsStore.Domain.Concrete +{ + public class EmailSettings + { + public string MailToAddress = "orders@example.com"; + public string MailFromAddress = "sportsstore@example.com"; + public bool UseSsl = true; + public string Username = "MySmtpUsername"; + public string Password = "MySmtpPassword"; + public string Servername = "smtp.example.com"; + public int ServerPort = 587; + public bool WriteAsFile = false; + public string FileLocation = @"c:\sports_store_emailss"; + } + + public class EmailOrderProcessor : IOrderProcessor + { + private EmailSettings emailSettings; + public EmailOrderProcessor(EmailSettings settings) + { + emailSettings = settings; + } + + public void ProcessOrder(Cart cart, ShippingDetails shippingInfo) + { + using (var smtpClient = new SmtpClient()) + { + smtpClient.EnableSsl = emailSettings.UseSsl; + smtpClient.Host = emailSettings.Servername; + smtpClient.Port = emailSettings.ServerPort; + smtpClient.UseDefaultCredentials = false; + smtpClient.Credentials = new NetworkCredential( + emailSettings.Username, + emailSettings.Password); + + if (emailSettings.WriteAsFile) + { + smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; + smtpClient.PickupDirectoryLocation = emailSettings.FileLocation; + smtpClient.EnableSsl = false; + } + StringBuilder body = new StringBuilder() + .AppendLine("A new order has been submitted") + .AppendLine("---") + .AppendLine("Items:"); + + foreach (var line in cart.Lines) + { + var subtotal = line.Product.Price * line.Quantity; + body.AppendFormat("{0} x {1} (subtotal: {2:c}", line.Quantity, line.Product.Name, subtotal); + } + + body.AppendFormat("Total order value: {0:c}", cart.ComputeTotalValue()) + .AppendLine("---") + .AppendLine("Ship to:") + .AppendLine(shippingInfo.Name) + .AppendLine(shippingInfo.Line1) + .AppendLine(shippingInfo.Line2 ?? "") + .AppendLine(shippingInfo.Line3 ?? "") + .AppendLine(shippingInfo.City) + .AppendLine(shippingInfo.State ?? "") + .AppendLine(shippingInfo.Country) + .AppendLine(shippingInfo.Zip) + .AppendLine("---") + .AppendFormat("Gift wrap: {0}", shippingInfo.GiftWrap ? "Yes" : "No"); + + MailMessage mailMessage = new MailMessage( + emailSettings.MailFromAddress, // From + emailSettings.MailToAddress, // To + "New order submitted!", // Subject + body.ToString()); // Body + + if (emailSettings.WriteAsFile) + { + mailMessage.BodyEncoding = Encoding.ASCII; + } + + smtpClient.Send(mailMessage); + } + + } + } +} diff --git a/SportsStore/SportsStore.Domain/SportsStore.Domain.csproj b/SportsStore/SportsStore.Domain/SportsStore.Domain.csproj index c22dbda..698e600 100644 --- a/SportsStore/SportsStore.Domain/SportsStore.Domain.csproj +++ b/SportsStore/SportsStore.Domain/SportsStore.Domain.csproj @@ -76,6 +76,7 @@ +