我正在制作一个通过 CDO 以 HTML 格式发送电子邮件的 JScript 脚本。我在 .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;
}
//-------------------------------------------
谢谢。