应用场景:
女友工作因素,需要定期的与客户发送组织会议邀请,发送对象不同,内容基本相同,附件不同,每次发送时麻烦费事耽误时间,此工具的开发应用必然会深受女友的喜欢、大大减少其工作量。
设计:
Excel表对应,收件人对象信息,遍历该对象列表,组织邮件内容,选中附件列表,一键发送并展示发送结果。
运行环境:
邮件服务器:163.com stmp方式
步骤:
- 读取excel中收件人列表,转存到内存DataTable中
- 邮件正文采用html格式,可以发送图片,保存预定好的邮件格式
- 根据邀请函模板生成每个人的邀请函doc文件,转换格式为pdf文件
- 遍历DataTable收件人,添加附件发送邮件
- 个人邮箱设置,发送步骤、进度显示
程序代码:
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
{