stmp批量发送email包括图片和附件

应用场景:
女友工作因素,需要定期的与客户发送组织会议邀请,发送对象不同,内容基本相同,附件不同,每次发送时麻烦费事耽误时间,此工具的开发应用必然会深受女友的喜欢、大大减少其工作量。
设计:
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;

                //负责列头Start
                DataColumn 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中——End
                return 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
            {
                
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值