2012-06-15 64 views
0

我正在製作一個JScript腳本,通過CDO以HTML格式發送電子郵件。我在.html文件中有一個消息模板,並且我將一些獨特的數據放入每個正在發送的消息中。因此,我爲每封郵件創建了原始.html文件的副本。CDO CreateMHTMLBody在JScript中鎖定文件

問題是,我無法在發送消息後從腳本內刪除此臨時文件。我使用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; 
} 
//------------------------------------------- 

感謝。

回答

0

你嘗試編程查殺郵件prcess,然後刪除該文件(除非你需要的郵件程序運行你發送的郵件後)

你已經安裝了IIS SMTP服務,可能是值得嘗試

的CreateObject( 「cdonts.NewMail」)

的CreateObject( 「outlook.application」)

但是,您可能必須在類似的情況下殺死相關進程。