C# .NET Html代码生成Word并发送Email
1.功能:
我要做的一个功能就是点击【发送邮件】按钮,生成一个word文档并发送邮件(但是图片需要获取到图片并转成Base64才可以加载到图片,等我写完后会在后面续加上方法)
2.功能概括:
本文发送Email的方法使用了两种生成Word的方式:
(1)System.IO.File.WriteAllText(Server.MapPath(wordPath), htmlCode);
wordPath:自己指定的word将要生成的文件路径
htmlCode:拼好的html源码
(2)写好的word模板,使用域方式插入数据
3.代码块(一共三个方法)
SendEmail(发送Email方法)
1 ///2 /// 发送Email 3 /// 4 /// 记录编号 5 /// 收件人CODE 6 public JsonResult SendEmail(string BILL_NO, string JOB_MAIL) 7 { 8 JsonResult JsonResult = new JsonResult(); 9 JsonResultInfo result = new JsonResultInfo(); 10 try 11 { 12 #region 附件Word模板寫入數據 13 WF_MEETINGEntity model = wmee.GetModel(BILL_NO); 14 byte[] byteArray = (byte[])model.REMARK; 15 var strREMARK = System.Text.Encoding.UTF8.GetString(byteArray); 16 //最终要发送的文件地址 17 string path = ""; 18 var wordPath = ""; 19 if (strREMARK.IndexOf("{\\rtf") < 0) 20 { 21 #region REMARK为RTF格式处理方法(html富文本框内容处理) 22 //将数据整理到视图模板 23 var viewModel = AssignmentModel(model); 24 //创建完整的html源码 25 var htmlCode = CreateHtml(viewModel); 26 27 //创建html文件路径 28 var htmlBasePath = "/File/SendEmail/REMARK"; 29 30 //这一块原本的思路是生成html文件,下一步再转为word文档(但是html文件转为word文档的思路很难实现,所以放弃了) 31 //var htmlPath = Path.Combine(htmlBasePath, "REMARK.html"); 32 //string filePath = Server.MapPath(htmlPath); 33 //using (StreamWriter sw = new StreamWriter(filePath)) 34 //{ 35 // sw.Write(htmlCode); 36 //} 37 38 //如果创建一个新文档并保存是这样写的: 39 wordPath = Path.Combine(htmlBasePath, "會議記錄.doc"); 40 System.IO.File.WriteAllText(Server.MapPath(wordPath), htmlCode); 41 #endregion 42 path = Server.MapPath(wordPath); 43 } 44 else 45 { 46 #region REMARK为RTF格式处理方法 47 RichTextBox rtBox = new RichTextBox(); 48 rtBox.Rtf = strREMARK; 49 var REMARK = rtBox.Text; 50 51 var templatePath = ""; 52 var templateBasePath = "/File/SendEmail"; 53 templatePath = Path.Combine(templateBasePath, "templePrn.doc"); 54 55 var doc = new Aspose.Words.Document(Server.MapPath(templatePath)); 56 string nowFile = "/File/SendEmail/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "templePrn.doc";//新文件的路径 57 58 string[] fieldNames = {"BILLNO", 59 "CONFIRMDT", 60 "HOSTBY", 61 "STARTDATE", 62 "STARTTIME", 63 "ENDDATE", 64 "ENDTIME", 65 "RECORDER", 66 "ADDRESS", 67 "ATTENMEMBER", 68 "ABSENCEMEMBER", 69 "MEETINGTITLE", 70 "REMARK", 71 "ATTENMEMBER1", 72 "ABSENCEMEMBER2"}; 73 string[] values = { model.BILL_NO, 74 model.CONFIRM_DT, 75 model.HOST_BY, 76 model.START_DATE, 77 model.START_TIME, 78 model.END_DATE, 79 model.END_TIME, 80 model.RECORDER, 81 model.ADDRESS, 82 model.ATTEN_MEMBER, 83 model.ABSENCE_MEMBER, 84 model.MEETING_TITLE, 85 REMARK, 86 model.ATTEN_MEMBER, 87 model.ABSENCE_MEMBER}; 88 89 doc.MailMerge.Execute(fieldNames, values); 90 doc.Save(Server.MapPath(nowFile)); 91 path = Server.MapPath(nowFile); 92 #endregion 93 } 94 95 List<string> list = new List<string>(); 96 list.Add(path); 97 #endregion 98 99 #region 獲取收件人 100 List listParms = new List (); 101 string where = ""; 102 if (!string.IsNullOrWhiteSpace(JOB_MAIL)) 103 { 104 where += " EMP_CODE IN('" + JOB_MAIL + "')"; 105 listParms.Add(new OracleParameter(":JOB_MAIL", JOB_MAIL)); 106 } 107 108 OracleParameter[] cmdParms = listParms.ToArray(); 109 List userModel = sye.GetModelList(where, cmdParms); 110 111 #endregion 112 113 #region 邮件内容 114 string body = @"Dear All: 115 116 會議記錄編號:[ " + model.BILL_NO + @"] 117 118 會議主題:" + model.MEETING_TITLE + @" 119 120 121 詳細內容請見附件!! 122 123 124 125 126 127 128 發送人:" + Li.UserEntity.USER_CODE + @" 129 發送時間:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + @" 130 131 請不用回復!!! 132 -------------------------------- 133 134 135 136 137 138 "; 139 string to = ""; 140 for (int i = 0; i < userModel.Count; i++) 141 { 142 if (i == 0) 143 { 144 to = userModel[i].EMP_EMAIL1; 145 } 146 else 147 { 148 to += ";" + userModel[i].EMP_EMAIL1; 149 } 150 } 151 #endregion 152 //to = "lucy_liu@soonest.com"; 153 new EmailHepler().Send("某某某", body, "", to, "", "", false, list); 154 System.IO.File.Delete(Server.MapPath(wordPath)); 155 156 157 } 158 catch (Exception ex) 159 { 160 result.IsSuccess = false; 161 result.ErrorMsg = ex.Message; 162 } 163 JsonResult.Data = result; 164 return JsonResult; 165 }
AssignmentModel(将数据整理到视图模板方法)
1 ///2 /// 将数据整理到视图模板 3 /// 4 /// 实体类 5 public ViewModel AssignmentModel(WF_MEETINGEntity modelEntity) 6 { 7 ViewModel ViemMod = new ViewModel(); 8 ViemMod.BILLNO = modelEntity.BILL_NO; 9 ViemMod.CONFIRMDT = modelEntity.CONFIRM_DT; 10 ViemMod.HOSTBY = modelEntity.HOST_BY; 11 var times = ""; 12 if (!string.IsNullOrEmpty(modelEntity.START_DATE)) 13 { 14 times = DateTime.ParseExact(modelEntity.START_DATE, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd"); 15 } 16 if (!string.IsNullOrEmpty(modelEntity.START_TIME)) 17 { 18 times = times.Trim() + " " + modelEntity.START_TIME.Substring(0, 2) + ":" + modelEntity.START_TIME.Substring(2, 2); 19 } 20 if (!string.IsNullOrEmpty(modelEntity.END_DATE)) 21 { 22 times = times.Trim() + " 到 " + DateTime.ParseExact(modelEntity.END_DATE, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd"); 23 } 24 if (!string.IsNullOrEmpty(modelEntity.END_TIME)) 25 { 26 times = times.Trim() + " " + modelEntity.END_TIME.Substring(0, 2) + ":" + modelEntity.END_TIME.Substring(2, 2); 27 } 28 ViemMod.MEENTINGTIME = times; 29 ViemMod.RECORDER = modelEntity.RECORDER; 30 ViemMod.ADDRESS = modelEntity.ADDRESS; 31 ViemMod.ATTENMEMBER = modelEntity.ATTEN_MEMBER; 32 ViemMod.ABSENCEMEMBER = modelEntity.ABSENCE_MEMBER; 33 ViemMod.MEETINGTITLE = modelEntity.MEETING_TITLE; 34 string REMARK = ""; 35 if (!string.IsNullOrWhiteSpace(modelEntity.REMARK?.ToString())) 36 { 37 string strREMARK = ""; 38 byte[] byteArray = (byte[])modelEntity.REMARK; 39 strREMARK = System.Text.Encoding.UTF8.GetString(byteArray); 40 if (strREMARK.IndexOf("{\\rtf") < 0) 41 { 42 REMARK = strREMARK; 43 } 44 else 45 { 46 System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox(); 47 rtBox.Rtf = strREMARK; 48 string plainText = rtBox.Text; 49 REMARK = plainText; 50 } 51 } 52 ViemMod.REMARK = REMARK; 53 ViemMod.ATTENMEMBER1 = modelEntity.ATTEN_MEMBER; 54 ViemMod.ABSENCEMEMBER2 = modelEntity.ABSENCE_MEMBER; 55 return ViemMod; 56 }
CreateHtml(创建完整的html源码)
这个就不详细贴源码了,毕竟每个人页面不一样意义不大,就是在html页面画好,在后台贴源码就是了。返回的是拼好的HTML源码
注意!头部标签需用这个(这个是网上百度的,参考的这个链接:https://www.cnblogs.com/wendj/p/6699885.html)
欢迎小伙伴们评论下方技术指导<(^_^)>