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 { /// <summary> /// 收件人地址 /// </summary> public string ToAddress { get; set; } /// <summary> /// 发件人地址 /// </summary> public string FromAddress { get; set; } /// <summary> /// 显示名称 /// </summary> public string DisplayName { get; set; } /// <summary> /// 邮件主题 /// </summary> public string Subject { get; set; } /// <summary> /// SMTP服务器地址 /// </summary> public string SmtpHost { get; set; } /// <summary> /// SMTP服务器端口 /// </summary> public int SmtpPort { get; set; } /// <summary> /// SMTP服务器用户名 /// </summary> public string SmtpUserName { get; set; } /// <summary> /// SMTP服务器密码 /// </summary> public string SmtpPassword { get; set; } /// <summary> /// 邮件正文内容 /// </summary> public string Body { get; set; } /// <summary> /// 附件 附件名字必填 /// </summary> public Stream stream { get; set; } /// <summary> /// 附件名字:带后缀的 /// </summary> public string filename { get; set; } }
附件依赖于 NPOI public static MemoryStream ExportCompanyOrder() { List<car_company_orderEntity> list = new List<car_company_orderEntity>(); 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 < list.Count(); 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 = "[email protected]"; mailInfo.SmtpPassword = "cheng1993"; mailInfo.DisplayName = "智领"; mailInfo.FromAddress = "[email protected]"; mailInfo.ToAddress = "[email protected]"; mailInfo.Subject = "测试"; mailInfo.Body = "测试的啊 "; mailInfo.filename = "111.xls"; mailInfo.stream = ExportCompanyOrder(); var aa = SendEmail(mailInfo);
demo 用来测试,以及发送。 注意:163后端需要在 开启客户端授权密码
原文地址:https://www.cnblogs.com/ichengq/p/10679232.html
时间: 2024-10-13 10:59:23