C# 163郵箱發送 附件以及郵件信息
阿新 • • 發佈:2019-04-09
標題 message edi 服務器端 pan 服務器密碼 cli div nat
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後端需要在 開啟客戶端授權密碼
C# 163郵箱發送 附件以及郵件信息