将生成的word文档,作为mail的附件发送。报错!
[size=medium]
现象简述:
将生成的word文档,作为mail的附件发送给客服部门。连续点击发送按钮报错。一般点击到第2,3次时报错。(winform)
在发现这个错误时我有点不解因为
a.c#生成word开发过,
b.c#发送mail开发过。
c.二者结合是再正常不过的业务需求也开发过(控制台程序)
纠结…………
好了找错吧!
主要程序代码
fileName = Func_cfg.getBakPath() + fileName; string dotName = Func_cfg.getTemplatePath() + "工单.dot"; WordUtility wordUtility = new WordUtility(dotName); wordUtility.createWordDocment(); wordUtility.replaceItems(ht); wordUtility.saveAs(@fileName);//这句报错 文件只读 wordUtility.close(); //发送报表 SendReport(fileName);
wordUtility.close();wordUtility类的close方法如下 public void close() { object o = false; oDoc.Close(ref o, ref oMissing, ref oMissing); oDoc = null; oWord.Quit(ref o, ref oMissing, ref oMissing); oWord = null; }查看任务管理器,在错误发生前,没有未关闭的进程。 又查了一下参数的意义,觉得问题不在这里。
fileName = Func_cfg.getTempPath() + fileName; string bakFilePath = Func_cfg.getBakPath() + fileName; string dotName = Func_cfg.getTemplatePath() + "工单.dot"; WordUtility wordUtility = new WordUtility(dotName); wordUtility.createWordDocment(); wordUtility.replaceItems(ht); wordUtility.saveAs(@fileName); wordUtility.close(); SendReport(fileName); MyUtility.copyFile(fileName, bakFilePath, true); MyUtility.deleFile(fileName);//这句报错 说文件被打开
MyUtility.deleFile(fileName);
wordUtility.close();MyUtility.deleFile(fileName);//断点放在这句SendReport(fileName);
sMTPUtility.addAttachment(fullFileName);
MailMessage message = new MailMessage ();message .Dispose();
this.message.Attachments[i].ContentStream.Close();