2014-01-23 59 views
0

我使用下面的VB腳本發送郵件。當我從命令提示符執行它時,它會在執行後返回到命令提示符。如果發送時有任何錯誤,則會顯示一條彈出消息。VB腳本發送郵件 - 等到發送郵件

If WScript.Arguments.Count <> 6 then 
Wscript.Echo "Missing parameters" 
Else 
Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = Wscript.Arguments(0) 
objMessage.From = Wscript.Arguments(1) 
objMessage.To = Wscript.Arguments(2) 
objMessage.TextBody = Wscript.Arguments(3) 
objMessage.AddAttachment Wscript.Arguments(4) 
objMessage.AddAttachment Wscript.Arguments(5) 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp server ip" 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
objMessage.Configuration.Fields.Update 
objMessage.Send 
End If 

我想知道在程序中發送電子郵件並獲取objMessage.Send的狀態需要多少時間。

我想使用Wscript.Echo「錯誤消息」顯示錯誤。上述代碼需要進行哪些更改才能執行,直到電子郵件成功發送並顯示一條消息。

在此先感謝。 阿肖克

回答

1

Send運行同步,即,它只返回如果

  • 發生錯誤
  • SMTP服務器拒絕該消息
  • SMTP服務器接受了消息遞送

Send啓用錯誤處理並檢查是否發生錯誤:

On Error Resume Next 
objMessage.Send 
If Err Then 
    WScript.Echo Err.Number & vbTab & Err.Description 
Else 
    WScript.Echo "Success" 
End If 
On Error Goto 0