測試代碼: 不忘初心,方得始終;初心易得,始終難守。 ...
/// <summary> /// 一人一附件發送郵件 /// 2017-05-17 塗聚文 GeovinDu /// </summary> /// <param name="To">接收郵件人</param> /// <param name="Subject">郵件標題</param> /// <param name="Msg">郵件內容</param> /// <param name="bodyHtml">是否網頁</param> /// <param name="AttachmentStream"></param> /// <param name="AttachmentFileName">附件</param> /// <returns></returns> public static bool SendEmaildu(string To, string Subject, string Msg, bool bodyHtml, MemoryStream AttachmentStream,string AttachmentFileName) { bool SendOk = false; try { System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg); newMsg.BodyEncoding = System.Text.Encoding.UTF8; newMsg.HeadersEncoding = System.Text.Encoding.UTF8; newMsg.SubjectEncoding = System.Text.Encoding.UTF8; System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); //smtpClient.Credentials = new NetworkCredential("[email protected]","888888"); //smtpClient.Port = 25; //smtpClient.EnableSsl = false; if (AttachmentStream != null && AttachmentFileName != null) { // ADD AN ATTACHMENT. //String sFile = @"C:\Source\LNS_IS.TXT"; //MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64); //string filePath = @"C:\TestEmail\SKYPE11月黑咭生日會員.xls"; //MemoryStream memStream = new MemoryStream(); //using (FileStream fileStream = File.OpenRead(filePath)) //{ // memStream.SetLength(fileStream.Length); // fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length); //} newMsg.Attachments.Add(new Attachment(AttachmentStream, AttachmentFileName)); //MediaTypeNames.Image.Jpeg) //System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName); //System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition; //disposition.FileName = AttachmentFileName; //disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment; //newMsg.Attachments.Add(attachment); } newMsg.IsBodyHtml = bodyHtml; smtpClient.Send(newMsg); SendOk = true; //return SENT_OK; } catch (Exception ex) { ex.Message.ToString(); AttachmentStream.Close(); SendOk = false; //"Error: " + ex.Message + "<br/><br/>Inner Exception: "+ ex.InnerException; } AttachmentStream.Close(); return SendOk; } /// <summary> /// 一人一附件發送郵件 /// 2017-05-17 塗聚文 GeovinDu /// </summary> /// <param name="To">接收郵件人</param> /// <param name="Subject">郵件標題</param> /// <param name="Msg">郵件內容</param> /// <param name="bodyHtml">是否網頁</param> /// <param name="AttachmentFileName">附件</param> /// <returns></returns> public static bool SendEmaildu(string To, string Subject, string Msg, bool bodyHtml, string AttachmentFileName) { MemoryStream AttachmentStream = new MemoryStream(); bool SendOk = false; try { System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg); newMsg.BodyEncoding = System.Text.Encoding.UTF8; newMsg.HeadersEncoding = System.Text.Encoding.UTF8; newMsg.SubjectEncoding = System.Text.Encoding.UTF8; System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); //smtpClient.Credentials = new NetworkCredential("[email protected]","888888"); //smtpClient.Port = 25; //smtpClient.EnableSsl = false; if (AttachmentStream != null && AttachmentFileName != null) { // ADD AN ATTACHMENT. //String sFile = @"C:\Source\LNS_IS.TXT"; //MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64); //string filePath = @"C:\TestEmail\SKYPE11月黑咭生日會員.xls"; //MemoryStream memStream = new MemoryStream(); //using (FileStream fileStream = File.OpenRead(filePath)) //{ // memStream.SetLength(fileStream.Length); // fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length); //} newMsg.Attachments.Add(new Attachment(AttachmentStream, AttachmentFileName)); //MediaTypeNames.Image.Jpeg) //System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName); //System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition; //disposition.FileName = AttachmentFileName; //disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment; //newMsg.Attachments.Add(attachment); } newMsg.IsBodyHtml = bodyHtml; smtpClient.Send(newMsg); SendOk = true; //return SENT_OK; } catch (Exception ex) { ex.Message.ToString(); AttachmentStream.Close(); SendOk = false; //"Error: " + ex.Message + "<br/><br/>Inner Exception: "+ ex.InnerException; } AttachmentStream.Close(); return SendOk; } /// <summary> /// 一人多附件發送郵件 /// 2019-06-18 塗聚文 GeovinDu /// </summary> /// <param name="To">接收郵件人</param> /// <param name="Subject">郵件標題</param> /// <param name="Msg">郵件內容</param> /// <param name="bodyHtml">是否網頁</param> /// <param name="AttachmentFiles">多附件</param> /// <returns></returns> public static bool SendEmailduFiles(string To, string Subject, string Msg, bool bodyHtml, List<string> AttachmentFiles) { MemoryStream AttachmentStream = new MemoryStream(); bool SendOk = false; try { System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg); newMsg.BodyEncoding = System.Text.Encoding.UTF8; newMsg.HeadersEncoding = System.Text.Encoding.UTF8; newMsg.SubjectEncoding = System.Text.Encoding.UTF8; System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); //smtpClient.Credentials = new NetworkCredential("[email protected]","888888"); //smtpClient.Port = 25; //smtpClient.EnableSsl = false; if (AttachmentStream != null && AttachmentFiles != null) { foreach (string filename in AttachmentFiles) { newMsg.Attachments.Add(new Attachment(AttachmentStream, filename)); //MediaTypeNames.Image.Jpeg) } } newMsg.IsBodyHtml = bodyHtml; smtpClient.Send(newMsg); SendOk = true; } catch (Exception ex) { ex.Message.ToString(); AttachmentStream.Close(); SendOk = false; } AttachmentStream.Close(); return SendOk; } /// <summary> /// 多人多附件發送郵件 /// 不考慮統計每封郵件發送狀態(另外考慮完善代碼) /// 2019-06-18 塗聚文 GeovinDu /// </summary> /// <param name="Tos">多人接收郵件人</param> /// <param name="Subject">郵件標題</param> /// <param name="Msg">郵件內容</param> /// <param name="bodyHtml">是否網頁</param> /// <param name="AttachmentFiles">多附件</param> /// <returns></returns> public static bool SendEmailsAndFiles(List<string> Tos, string Subject, string Msg, bool bodyHtml, List<string> AttachmentFiles) { MemoryStream AttachmentStream = new MemoryStream(); bool SendOk = false; List<Attachment> all = new List<Attachment>(); try { if (AttachmentStream != null && AttachmentFiles != null) { foreach (string filename in AttachmentFiles) { all.Add(new Attachment(AttachmentStream, filename)); } } if (Tos.Count > 0) { foreach (string To in Tos) { System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg); newMsg.BodyEncoding = System.Text.Encoding.UTF8; newMsg.HeadersEncoding = System.Text.Encoding.UTF8; newMsg.SubjectEncoding = System.Text.Encoding.UTF8; System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); //smtpClient.Credentials = new NetworkCredential("[email protected]","888888"); //smtpClient.Port = 25; //smtpClient.EnableSsl = false; if (AttachmentStream != null && AttachmentFiles != null) { foreach (Attachment attname in all) { newMsg.Attachments.Add(attname); //MediaTypeNames.Image.Jpeg) } } newMsg.IsBodyHtml = bodyHtml; smtpClient.Send(newMsg); SendOk = true; } } } catch (Exception ex) { ex.Message.ToString(); AttachmentStream.Close(); SendOk = false; } AttachmentStream.Close(); return SendOk; }
測試代碼:
MemoryStream theMemStream = new MemoryStream(); string file = @"C:\TestEmail\SKYPE11月黑咭生日會員.xls"; //C:\TestEmail\Top1000WorldBanks2014.pdf if (File.Exists(file)) { //AttachmentStream = File.Open(file, FileMode.Open, FileAccess.Read); byte[] filebyte = MirosoftSmptHelper.ReadFile(file); theMemStream.Write(filebyte, 0, filebyte.Length);// = File.OpenRead(file); theMemStream.Position = 0; //theMemStream.Close(); // AttachmentStream = theMemStream;// BytesToStream(filebyte); //new MemoryStream(filebyte);// // BytesToStream(filebyte);// FileToStream(file);// //AttachmentStream.CanTimeout =true; //AttachmentStream.ReadTimeout = 1000; //AttachmentStream.WriteTimeout = 1000; //AttachmentStream = BytesToStream(filebyte); //theMemStream.Close(); } List<string> AttachmentFiles = new List<string>(); AttachmentFiles.Add("馬拉松獎牌刻字.xlsx"); AttachmentFiles.Add("進一步優化供給推動消費平穩增長2019.pdf"); AttachmentFiles.Add("逃犯條例草案.pdf"); AttachmentFiles.Add("複習捲14.doc"); List<string> Tos = new List<string>(); Tos.Add("[email protected]"); Tos.Add("[email protected]"); Tos.Add("[email protected]"); //1.一人一附件 //bool issend = MirosoftSmptHelper.SendEmaildu("[email protected]", "中文文件名.net 4.0,geovindu 塗聚文塗聚文工團、", "<p> 生命運浪漫滿屋geovindu江蘇黃河</p><br/><p>上官放個屁,下官一臺戲</p>", true,"馬拉松獎牌刻字.xlsx"); //2.一人多附件 //bool issend = MirosoftSmptHelper.SendEmailduFiles("[email protected]", "中文文件名.net 4.0,geovindu 塗聚文塗聚文工團、", "<p> 生命運浪漫滿屋geovindu江蘇黃河</p><br/><p>上官放個屁,下官一臺戲</p>", true, AttachmentFiles); //3.多人多附件 bool issend = MirosoftSmptHelper.SendEmailsAndFiles(Tos, "中文文件名.net 4.0,geovindu 塗聚文塗聚文工團、", "<p> 生命運浪漫滿屋geovindu江蘇黃河</p><br/><p>上官放個屁,下官一臺戲</p>", true, AttachmentFiles); theMemStream.Close(); if (issend) { MessageBox.Show("ok"); } else { MessageBox.Show("no"); } }
不忘初心,方得始終;初心易得,始終難守。