CDO CreateMHTMLBody使用JScript锁定文件

时间:2012-06-15 12:52:46

标签: vbscript jscript

我正在制作一个JScript脚本,通过CDO以HTML格式发送电子邮件。我在.html文件中有一个消息模板,我在发送的每条消息中都放了一些唯一的数据。因此,我为每条消息创建了原始.html文件的副本。

问题是我在发送邮件后无法从脚本中检测到这个临时文件。我已经检查了ProcessExplorer,该进程锁定了文件,它是cscript.exe / wscript.exe(用于运行脚本)。

以下是示例(简化)代码:

//-------------------------------------------
var fso = new ActiveXObject("Scripting.FileSystemObject");
var BodyFileName = fso.GetAbsolutePathName(WScript.Arguments(3));
var BodyExtensionName = fso.GetExtensionName(BodyFileName)
var BodyFile = fso.OpenTextFile(BodyFileName, 1);
var Body = BodyFile.ReadAll();
BodyFile.Close();
//-------------------------------------------
// Replace something in Body here
//-------------------------------------------
BodyFileName = fso.BuildPath(fso.GetParentFolderName(BodyFileName), fso.GetTempName() + "." + BodyExtensionName);
var TmpBodyFile = fso.OpenTextFile(BodyFileName, 2, true);
TmpBodyFile.Write(Body);
TmpBodyFile.Close();
// Here ProcessExplorer shows that the file is closed
//-------------------------------------------
var objEmail = new ActiveXObject("CDO.Message");  
objEmail.From = WScript.Arguments(0);
objEmail.To = WScript.Arguments(1);  
objEmail.Subject = WScript.Arguments(2);  
objEmail.CreateMHTMLBody("file:///" + BodyFileName.replace("\\","/"));
// Here the file is open again
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;  
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.1";
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25;  
objEmail.Configuration.Fields.Update();  
objEmail.Send();
objEmail = null;
//-------------------------------------------
// The following loop becomes endless
for (;;) {
  try {
    fso.DeleteFile(BodyFileName, true);
  } catch(e) {
    WScript.Sleep(100);
    continue;
  } finally {
  }
  break;
}
//-------------------------------------------

感谢。

1 个答案:

答案 0 :(得分:0)

您是否尝试以编程方式终止邮件进程然后删除文件(除非您在发送邮件后需要运行邮件程序)

您是否安装了IIS SMTP服务,可能值得尝试

的CreateObject( “cdonts.NewMail”)

的CreateObject( “outlook.application”)

但是你可能不得不在类似情况下杀死相关进程。