C# 163邮箱发送 附件以及邮件信息

2021-06-08 20:05

阅读:395

标签:amp   mst   alt   mail   用户   get   lte   cell   lse   

 
public static bool SendEmail(EmailInfo mailInfo) { try { MailMessage mailMsg = new MailMessage(); mailMsg.To.Add(new MailAddress(mailInfo.ToAddress)); mailMsg.From = new MailAddress(mailInfo.FromAddress, mailInfo.DisplayName); mailMsg.Subject = mailInfo.Subject; mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(mailInfo.Body, Encoding.UTF8, MediaTypeNames.Text.Html)); SmtpClient smtpClient = new SmtpClient(mailInfo.SmtpHost, mailInfo.SmtpPort); System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(mailInfo.SmtpUserName, mailInfo.SmtpPassword); smtpClient.Credentials = credentials; if (!string.IsNullOrEmpty(mailInfo?.filename) && mailInfo?.stream != null) { mailMsg.Attachments.Add(new Attachment(mailInfo.stream, mailInfo.filename)); } smtpClient.Send(mailMsg); } catch (Exception ex) { return false; } return true; }
 public class EmailInfo
    {
        /// 
        /// 收件人地址
        /// 
        public string ToAddress { get; set; }
        /// 
        /// 发件人地址
        /// 
        public string FromAddress { get; set; }
        /// 
        /// 显示名称
        /// 
        public string DisplayName { get; set; }
        /// 
        /// 邮件主题
        /// 
        public string Subject { get; set; }
        /// 
        /// SMTP服务器地址
        /// 
        public string SmtpHost { get; set; }
        /// 
        /// SMTP服务器端口
        /// 
        public int SmtpPort { get; set; }
        /// 
        /// SMTP服务器用户名
        /// 
        public string SmtpUserName { get; set; }
        /// 
        /// SMTP服务器密码
        /// 
        public string SmtpPassword { get; set; }
        /// 
        /// 邮件正文内容
        /// 
        public string Body { get; set; }

        /// 
        /// 附件 附件名字必填
        /// 
        public Stream stream { get; set; }

        /// 
        /// 附件名字:带后缀的
        /// 
        public string filename { get; set; }
    }
附件依赖于 NPOI



public static MemoryStream ExportCompanyOrder() { List list = new List(); for (int i = 0; i 100; i++) { list.Add(new car_company_orderEntity() { car_company_order_id = i, company_name = "测试" + i }); } //创建Excel文件的对象 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); //添加一个sheet NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1"); //给sheet1添加第一行的头部标题 NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0); row1.CreateCell(0).SetCellValue("ID"); row1.CreateCell(1).SetCellValue("授权商名称"); //将数据逐步写入sheet1各个行 for (int i = 0; i ) { NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1); rowtemp.CreateCell(0).SetCellValue(list[i]?.car_company_order_id.ToString()); //ID rowtemp.CreateCell(1).SetCellValue(list[i]?.company_name); //授权商名称 } // 写入到客户端 System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); ms.Seek(0, SeekOrigin.Begin); return ms; }
  var mailInfo = new EmailInfo();
            mailInfo.SmtpHost = "smtp.163.com";
            //mailInfo.SmtpPort = 25;
            mailInfo.SmtpUserName = "ichengq@163.com";
            mailInfo.SmtpPassword = "cheng1993";
            mailInfo.DisplayName = "智领";
            mailInfo.FromAddress = "ichengq@163.com";
            mailInfo.ToAddress = "41753655@qq.com";
            mailInfo.Subject = "测试";
            mailInfo.Body = "测试的啊 ";
            mailInfo.filename = "111.xls";
            mailInfo.stream = ExportCompanyOrder();
            var aa = SendEmail(mailInfo);

demo 用来测试,以及发送。 注意:163后端需要在 开启客户端授权密码技术图片

 

C# 163邮箱发送 附件以及邮件信息

标签:amp   mst   alt   mail   用户   get   lte   cell   lse   

原文地址:https://www.cnblogs.com/ichengq/p/10679232.html


评论


亲,登录后才可以留言!