应用场景:
女友工作因素,需要定期的与客户发送组织会议邀请,发送对象不同,内容基本相同,附件不同,每次发送时麻烦费事耽误时间,此工具的开发应用必然会深受女友的喜欢、大大减少其工作量。
设计:
Excel表对应,收件人对象信息,遍历该对象列表,组织邮件内容,选中附件列表,一键发送并展示发送结果。
运行环境:
邮件服务器:163.com stmp方式
步骤:
  1. 读取excel中收件人列表,转存到内存DataTable中
  2. 邮件正文采用html格式,可以发送图片,保存预定好的邮件格式
  3. 根据邀请函模板生成每个人的邀请函doc文件,转换格式为pdf文件
  4. 遍历DataTable收件人,添加附件发送邮件
  5. 个人邮箱设置,发送步骤、进度显示
程序代码:
        string excelFilePath= "";string wordExFilePath = "";string wordFilePath = "";string senderEmail = null;string senderPassword = null;string emailSubject = "";string emailBody = "";bool isHaveImg = false;private void Form1_Load(object sender, EventArgs e){}private System.Data.DataTable GetExcelData(){Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();Microsoft.Office.Interop.Excel.Sheets sheets;Microsoft.Office.Interop.Excel.Workbook workbook = null;object oMissiong = System.Reflection.Missing.Value;System.Data.DataTable dt = new System.Data.DataTable();try{if (app == null)return null;workbook = app.Workbooks.Open(excelFilePath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);//将数据读入到DataTable中——Start   sheets = workbook.Worksheets;Worksheet worksheet = (Worksheet)sheets.get_Item(1);//读取第一张表if (worksheet == null)return null;string cellContent;int iRowCount = worksheet.UsedRange.Rows.Count;int iColCount = worksheet.UsedRange.Columns.Count;Microsoft.Office.Interop.Excel.Range range;//负责列头StartDataColumn dc;int ColumnID = 1;range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 1];while (range.Text.ToString().Trim() != ""){dc = new DataColumn();dc.DataType = System.Type.GetType("System.String");dc.ColumnName = range.Text.ToString().Trim();dt.Columns.Add(dc);range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, ++ColumnID];}for (int iRow = 2; iRow <= iRowCount; iRow++){DataRow dr = dt.NewRow();for (int iCol = 1; iCol <= iColCount; iCol++){range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, iCol];cellContent = (range.Value2 == null) ? "" : range.Text.ToString();dr[iCol - 1] = cellContent;}dt.Rows.Add(dr);}//将数据读入到DataTable中——Endreturn dt; }catch{return null;}finally{workbook.Close(false, oMissiong, oMissiong);System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);workbook = null;app.Workbooks.Close();app.Quit();System.Runtime.InteropServices.Marshal.ReleaseComObject(app);app = null;}}private bool Convert(string sourcePath, string findName, string replaceName){bool result;object paramMissing = Type.Missing;Word.ApplicationClass wordApplication = new Word.ApplicationClass();Word.Document wordDocument = null;try{object paramSourceDocPath = sourcePath;string paramExportFilePath = System.Environment.CurrentDirectory + @"\" + replaceName + @"\附件_邀请函.pdf";if (!Directory.Exists(System.Environment.CurrentDirectory + @"\" + replaceName)){Directory.CreateDirectory(System.Environment.CurrentDirectory + @"\" + replaceName);}Word.WdExportFormat paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;bool paramOpenAfterExport = false;Word.WdExportOptimizeFor paramExportOptimizeFor =Word.WdExportOptimizeFor.wdExportOptimizeForPrint;Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;int paramStartPage = 0;int paramEndPage = 0;Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentWithMarkup;bool paramIncludeDocProps = true;bool paramKeepIRM = true;Word.WdExportCreateBookmarks paramCreateBookmarks =Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;bool paramDocStructureTags = true;bool paramBitmapMissingFonts = false;bool paramUseISO19005_1 = false;wordDocument = wordApplication.Documents.Open(ref paramSourceDocPath, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing);if (wordDocument != null){#region 替换收件人object findtext = findName;object replacewith = replaceName;object replace= Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;wordApplication.Selection.Find.Execute(ref findtext, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing,ref replacewith, ref replace,ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing);wordDocument.Save();#endregion#region 导出pdfwordDocument.ExportAsFixedFormat(paramExportFilePath,paramExportFormat, paramOpenAfterExport,paramExportOptimizeFor, paramExportRange, paramStartPage,paramEndPage, paramExportItem, paramIncludeDocProps,paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,paramBitmapMissingFonts, paramUseISO19005_1,ref paramMissing);//导出pdf方法二//wordApplication.ActiveDocument.SaveAs2(ref Target, ref format,//  ref Unknown, ref Unknown, ref Unknown,//ref Unknown, ref Unknown, ref Unknown,//ref Unknown, ref Unknown, ref Unknown,//ref Unknown, ref Unknown, ref Unknown,//ref Unknown, ref Unknown, ref Unknown); #endregion}result = true;}catch (Exception ex){Console.WriteLine(ex.ToString());return false;}finally{if (wordDocument != null){wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);wordDocument = null;}if (wordApplication != null){wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);wordApplication = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();}return result;}private string ReadDocument(string sourcePath){object paramMissing = Type.Missing;Word.ApplicationClass wordApplication = new Word.ApplicationClass();Word.Document wordDocument = null;try{object paramSourceDocPath = sourcePath;object encoding = System.Text.Encoding.UTF8;wordDocument = wordApplication.Documents.Open(ref paramSourceDocPath, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing);return (wordDocument != null && wordDocument.Content != null && wordDocument.Content.Text != null) ? wordDocument.Content.Text : "";}catch (Exception ex){Console.WriteLine(ex.ToString());return "";}finally{if (wordDocument != null){wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);wordDocument = null;}if (wordApplication != null){wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);wordApplication = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();}}private string ReadModehtml(string sourcePath){string strhtmlContent = "";try{System.IO.StreamReader sr = new System.IO.StreamReader(sourcePath);strhtmlContent = sr.ReadToEnd();}catch (Exception ex){return "";}return strhtmlContent; }private bool SendEmail(string subject,string body,string receiveName,string receiveEmail){try{//SmtpClient下的一个对象,用以设置邮件的主题和内容System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();//发送端到接收端的邮箱地址myMail = new System.Net.Mail.MailMessage(senderEmail, receiveEmail);myMail.Subject = subject;myMail.Body = body =body.Replace("张三", receiveName);myMail.IsBodyHtml = true;myMail.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码if (!string.IsNullOrEmpty(receiveName)){//建立邮件附件类的一个对象,语法格式为System.Net.Mail.Attachment(文件名,文件格式)System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(System.Environment.CurrentDirectory + @"\" + receiveName + @"\附件_邀请函.pdf", System.Net.Mime.MediaTypeNames.Application.Octet);//MIME协议下的一个对象,用以设置附件的创建时间,修改时间以及读取时间//System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;//disposition.CreationDate = System.IO.File.GetCreationTime(file);//disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);//disposition.ReadDate = System.IO.File.GetLastAccessTime(file);//用smtpclient对象里attachments属性,添加上面设置好的myattachmentmyMail.Attachments.Add(myAttachment);}if (isHaveImg){AlternateView avHtml = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);LinkedResource pic1 = new LinkedResource("123.jpg", MediaTypeNames.Image.Jpeg);pic1.ContentId = "Pic1";avHtml.LinkedResources.Add(pic1);myMail.AlternateViews.Add(avHtml);}//建立发送对象client,验证邮件服务器,服务器端口,用户名,以及密码System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.163.com", 25);client.Credentials = new System.Net.NetworkCredential(senderEmail, senderPassword);client.Timeout = 1000 * 60 * 2;//2分钟//client.UseDefaultCredentials = false;client.Send(myMail);return true;}catch (Exception ex){return false;}}private void button1_Click(object sender, EventArgs e){//选择excel文件OpenFileDialog openFile = new OpenFileDialog();openFile.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);openFile.Multiselect = false;if (openFile.ShowDialog() == DialogResult.Cancel)return;excelFilePath = openFile.FileName;string fileType = System.IO.Path.GetExtension(excelFilePath);if (string.IsNullOrEmpty(fileType))return;this.textBox1.Text = excelFilePath;}private void button2_Click(object sender, EventArgs e){OpenFileDialog openFile = new OpenFileDialog();openFile.Filter = "Word document(*.doc)|*.doc|Word document(*.docx)|*.docx";openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);openFile.Multiselect = false;if (openFile.ShowDialog() == DialogResult.Cancel)return;wordExFilePath = openFile.FileName;string fileType = System.IO.Path.GetExtension(wordExFilePath);if (string.IsNullOrEmpty(fileType))return;this.textBox2.Text = wordExFilePath;}private void button3_Click(object sender, EventArgs e){//测试用户密码是否正常,给自己发一封邮件if (string.IsNullOrEmpty(this.textBox3.Text) || string.IsNullOrEmpty(this.textBox4.Text)){MessageBox.Show("笨蛋,邮箱的账户和密码都是必填项!", "Matin大叔提醒你", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;}string strContentExFile = ReadDocument(wordExFilePath);string strContentFile = ReadModehtml(wordFilePath);if (string.IsNullOrEmpty(strContentExFile) || string.IsNullOrEmpty(strContentFile) || !strContentExFile.Contains("张三") || !strContentFile.Contains("张三")){MessageBox.Show("笨蛋,邮件正文和附件都是必选项,同时都必须以 [张三] 为模板!", "Matin大叔提醒你", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;}if (!strContentExFile.Contains("张三")){MessageBox.Show("笨蛋,邮件附件文件都必须以 [张三] 为模板!", "Matin大叔提醒你", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;}if (!strContentFile.Contains("张三")){MessageBox.Show("笨蛋,邮件正文文件都必须以 [张三] 为模板!", "Matin大叔提醒你", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;}senderEmail = this.textBox3.Text.Trim();senderPassword = this.textBox4.Text.Trim();this.button3.Enabled = false;if (SendEmail("测试邮件,不必理我", ReadModehtml(wordFilePath), "", senderEmail)){textBox5.Text =  "邮件测试成功!";button4.Enabled = true;button3.Enabled = false;textBox4.Text = "******";}else{textBox5.Text = "<@.@>邮件发送测试失败,确保账户信息、网络是否正常!";this.button3.Enabled = true;}}private void button4_Click(object sender, EventArgs e){if (string.IsNullOrEmpty(excelFilePath) || string.IsNullOrEmpty(wordExFilePath) || string.IsNullOrEmpty(wordFilePath)){MessageBox.Show("笨蛋,excel和word文件都得选择完备!", "Matin大叔提醒你", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;}textBox5.Text += System.Environment.NewLine + "excel中收件人信息读取中......";System.Data.DataTable dt = GetExcelData();if (dt == null || dt.Rows.Count == 0){MessageBox.Show("笨蛋,excel文件是不是格式什么有问题,不要随意修改模板!", "Matin大叔提醒你", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;}button4.Enabled = false;textBox5.Text += System.Environment.NewLine + "excel中收件人信息读取完成!收件人合计人数 " + dt.Rows.Count;textBox5.Text += System.Environment.NewLine + "word邀请函模板读取完成!";textBox5.Text += System.Environment.NewLine + "word邀请函按照收件人副本制作中......";string findTextName = "张三";List<string> listFailedName = new List<string>();foreach (var DataRow in dt.Rows){DataRow row = DataRow as DataRow;string sName = row["姓名"].ToString();string sEmail = row["邮箱"].ToString();if (Convert(wordExFilePath, findTextName, sName)){textBox5.Text += System.Environment.NewLine + string.Format("pdf附件邀请函 [{0}] 已生成成功!", sName);findTextName = sName;}else{textBox5.Text += System.Environment.NewLine + string.Format("<@.@>pdf附件邀请函 [{0}] 生成失败,不过,没关系等最后结束后重试!", sName);listFailedName.Add(sName);}}if (listFailedName != null && listFailedName.Count > 0){foreach (var item in listFailedName){LogHelper.CreateLog(string.Format("pdf附件邀请函 [{0}] 生成失败,不过,没关系等最后结束后重试!", item));}}textBox5.Text += System.Environment.NewLine + string.Format("pdf附件邀请函生成已结束!");textBox5.Text += System.Environment.NewLine + string.Format("邮件开始发送中......");List<string> listsendFailedName = null;listsendFailedName = new List<string>();foreach (var DataRow in dt.Rows){DataRow row = DataRow as DataRow;string sName = row["姓名"].ToString();string sEmail = row["邮箱"].ToString();if (listFailedName != null && listFailedName.Count > 0 && listFailedName.Contains(sName)) ;else{string docContent = ReadModehtml(wordFilePath);if (!string.IsNullOrEmpty(docContent)){emailSubject = "测试测试主题";emailBody = docContent;}if (SendEmail(emailSubject, emailBody, sName, sEmail)){textBox5.Text += System.Environment.NewLine + string.Format("邮件 [{0}] 发送成功!", sName);}else{textBox5.Text += System.Environment.NewLine + string.Format("<@.@>邮件 [{0}] 发送失败了,不过,没关系等最后结束后重试!", sName);listsendFailedName.Add(sName);}}}if (listsendFailedName != null && listsendFailedName.Count > 0){foreach (var item in listsendFailedName){LogHelper.CreateLog(string.Format("邮件 [{0}] 发送失败了,不过,没关系等最后结束后重试!", item));}}textBox5.Text += System.Environment.NewLine + string.Format("邮件发送已结束!");if ((listFailedName != null && listFailedName.Count > 0) || (listsendFailedName!=null && listsendFailedName.Count>0))textBox5.Text += System.Environment.NewLine + string.Format("<@.@>邮件发送过程中出现异常,查看异常日志联系Martin大叔");listFailedName = null;listsendFailedName = null;}private void button5_Click(object sender, EventArgs e){OpenFileDialog openFile = new OpenFileDialog();openFile.Filter = "Html(*.html)|*.html|Htm(*.htm)|*.htm";openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);openFile.Multiselect = false;if (openFile.ShowDialog() == DialogResult.Cancel)return;wordFilePath = openFile.FileName;string fileType = System.IO.Path.GetExtension(wordFilePath);if (string.IsNullOrEmpty(fileType))return;this.textBox6.Text = wordFilePath;}private void radioButton2_CheckedChanged(object sender, EventArgs e){if (radioButton2.Checked)isHaveImg = label5.Visible = true;elseisHaveImg = label5.Visible = false;}

参考资料:
http://www.cnblogs.com/Joetao/articles/2933941.html
http://q.cnblogs.com/q/32301/
https://social.msdn.microsoft.com/Forums/office/en-US/b81ff74a-9c22-403c-b62c-5e1aba5106bd/microsoftofficeinteropwordapplicationdocumentsopen-issue-with-filename?forum=worddev
http://www.cnblogs.com/cfcool/archive/2010/02/05/1664145.html
http://bbs.csdn.net/topics/100094640
http://blog.csdn.net/kkkkkxiaofei/article/details/7941239
http://blog.csdn.net/tolearner/article/details/6102023
http://www.jb51.net/article/63486.htm
http://www.jb51.net/article/55465.htm
程序下载

stmp批量发送email包括图片和附件相关推荐

  1. C#批量发送、抄送邮件、附件

    ** 以下代码发送效果在网易和腾讯邮箱显示不一样 C#批量发送.抄送.密送.回执.邮件.附件 ** 代码方法 using System; using System.Net.Mail; using Sy ...

  2. java发送html附件_Java发送邮件(图片、附件、HTML)

    一.简介 要在网络上实现邮件功能,必须要有专门的邮件服务器.这些邮件服务器类似于现实生活中的邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中. SMTP服务器地址: 一般是 ...

  3. java发送图片_Java发送邮件(图片、附件、HTML)

    一.简介 要在网络上实现邮件功能,必须要有专门的邮件服务器.这些邮件服务器类似于现实生活中的邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中. SMTP服务器地址: 一般是 ...

  4. 使用Python实现微信发送文本消息、图片以及附件

    使用Python实现微信发送文本消息.图片以及附件 因为目前很多微信账号已经不支持网页版的操作了,因此无法使用之前的itchat这一类对微信进行操作. 为此,选择对微信PC端进行开发. 选择的方式比较 ...

  5. 批量发送邮件(java开发、带图片和附件)

    本程序配置好后可以直接运行,采用javamail1.6 api ,具有如下特点: (1)邮件内容.主题 可以定制,内容可以是富文本,可以带图片和附件 (2)为了避免垃圾邮件拦截,设置为3秒钟一封,每发 ...

  6. postman + node-red 发送e-mail(附件、抄送、密送)

    postman 调用 node-red发送邮件 node-red发送e-mail 环境准备 Postman参数参考 node-red节点参考 邮件发送成功截图 node-red源文件 node-red ...

  7. word合并邮件无法发送html,Word邮件合并批量发送带附件的邮件

    应张老师的需求,修改制作了一个可以批量发送带附件的电子邮件VBA. 目的:给N多人发送电子邮件,而不是抄送模式,并带有对方的称谓. 实现: 用到Word的邮件合并功能,以及调用Outlook发送邮件. ...

  8. Java Email以及发送Email附件

    我们首先了解一下传统的邮件是如何进行发送的,首先把邮件发送到当地的邮局,当地的邮局又把邮件发送给目的地附近的邮局,最后目的地附近的邮局把邮件发送到目的地. 其实Email的发送方式也是类似,我们把类似 ...

  9. oracle util_mail,ORACLE UTL_MAIL 发送Email 无附件 有附件

    [那些ORCALE DB可以做到的事] 如果你的DB是 ORACLE 10g 以上 那么就可以使用 package UTL_MAIL 提供的功能, 发邮件只需要一步就可以做到. 但是前期需要做一些DB ...

最新文章

  1. Linux搭建Maven私服, 使项目公用Android aar
  2. C中常用字符串处理函数
  3. Windows Server 2008十大新特性
  4. cisco packet tracer实验案例-重置路由器ENABLE特权密码
  5. HQL中左连接,右连接、内连接
  6. jbpm视频教程全集下载【21讲完整】
  7. redis 在 mac 下的安装与使用
  8. swift5 decimal 转换为String 并制定小数点位数
  9. win10电脑系统里的视频不显示缩略图
  10. WIFI参数Beacon Interval、DTIM、Fragment Length、RTS Length
  11. jmeterhttp代理服务器_Jmeter使用HTTP代理服务器录制脚本
  12. 五家渠市谷歌高清卫星地图下载
  13. pytorch导出onnx格式模型时,不固定输入输出维度
  14. 看完这篇招聘方法论,90%CEO会心痛
  15. 平安科技美国研究院院长韩玫:AI赋能传统行业,要知其然也知其所以然
  16. 爱词霸汉语站联合多家官方媒体发布中国十大流行语
  17. http、https 等 常用默认端口号
  18. Java实现-数字组合1
  19. 认知科学期末复习笔记
  20. windows消息处理过程及消息钩子

热门文章

  1. 观察者和中介者的区别
  2. 【xtku】熟知idown万用下载器的使用办法
  3. PowerBuilder 使用statictext创建可改变背景色的按钮
  4. 【信息系统项目管理师】2019下半年信息系统项目管理师考试案例分析
  5. 市场营销常用理论【转】
  6. 百度引擎的伴侣是SEO吗?
  7. 【引用】雨林木风Ghost XP SP3系统
  8. 地震捕手--通过传感器技术和计算机网络检测地震
  9. ThinkPad T410加单条8G协德内存条成功
  10. 联想台式机 Thinkcentre M8000T bios设置从U盘启动的方法