zoukankan      html  css  js  c++  java
  • 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 = 587;                        //服务器端口号
        }
  • 相关阅读:
    Windows下不能启动mysql服务错误总结
    使用NSOperationQueue简化多线程开发(转)
    “四人帮”的设计模式经得起时间的考验么?(转)
    ObjectiveC category
    svn add 输出 A (bin) (转)
    NSNotification学习笔记
    浅析UITableViewCell的工作机制
    关于git分支的使用
    delegate使用方法之assign
    ARC(Automatic Reference Counting )技术概述(转)
  • 原文地址:https://www.cnblogs.com/zhangchaoran/p/7803825.html
Copyright © 2011-2022 走看看