SMTP 发邮件

    public class EmailOrderProcessor :IOrderProcessor
{
private EmailSettings es; public EmailOrderProcessor(EmailSettings settings)
{
es = settings;
} public void ProcessOrder(Cart cart,ShippingDetails shippingInfo)
{
using (var smtpClient = new SmtpClient())
{
smtpClient.EnableSsl = true;
smtpClient.Host = es.ServerName; //服务器地址
smtpClient.Port = es.ServerPort; //端口号
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(es.Username, es.Password); //设置用于验证发件人身份的凭证 //声明一个可变字符串变量 body,用来存储邮件内容
StringBuilder body = new StringBuilder().AppendLine("您有新的订单,请注意查收")
.AppendLine("------")
.AppendLine("订单信息:"); foreach(var line in cart.Lines)
{
var subtotal = line.Product.Price * line.Quantity;
body.AppendFormat("{0} x {1} (总价:{2:c})", line.Quantity, line.Product.Name, subtotal);
} body.AppendFormat("合计:{0:c}", cart.ComputeTotalValue())
.AppendLine("------")
.AppendLine("送到:")
.AppendLine(shippingInfo.Name)
.AppendLine(shippingInfo.Line1)
.AppendLine(shippingInfo.Line2 ?? " ")
.AppendLine(shippingInfo.Line3 ?? "")
.AppendLine(shippingInfo.Province + " ")
.Append(shippingInfo.City)
.AppendLine(shippingInfo.Zip)
.AppendLine("------")
.AppendFormat("包装:{0}", shippingInfo.GiftWrap ? "需要" : "不需要"); //发件人,收件人,邮件标题,邮件内容
MailMessage mailMessage = new MailMessage(es.MailFromAddress, es.MailToAddress, "新的订单", body.ToString());
//发送邮件
smtpClient.Send(mailMessage);
}
}
} public class EmailSettings
{
public string MailToAddress = "11478@qq.com"; //收件箱(这里因为是给管理员发,所以是固定的)
public string MailFromAddress = "sportsstore@example.com"; //发件箱 public string Username = "MySmtpUsername"; //账户名
public string Password = "MySmtpPassword"; //密码(授权码)
public string ServerName = "smtp.example.com"; //SMTP服务器地址
public int ServerPort = ; //服务器端口号
}
上一篇:2018.4.18 Ubuntu 的telnet命令详解


下一篇:.net core中使用openssl的公钥私钥进行加解密