我编写了一个自动测试,该测试每天晚上运行,测试结束后,我想每天晚上通过电子邮件发送结果。

为此,我尝试将以下内容放在批处理文件的末尾:

Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0)
With MyItem
    .To = "a@a.com"
    .Subject = "Subject"
    .ReadReceiptRequested = False
    .HTMLBody = "resport"
End With
MyItem.Send


但是,这导致电子邮件无法发送,因为未打开Outlook,因为该测试在后台运行,并且我无法访问UI。

无论如何,在没有实际运行Outlook的情况下发送此电子邮件。

谢谢!

最佳答案

您可以使用CDO.Message对象在VBScript中发送没有Outlook的电子邮件。您将需要知道SMTP服务器的地址才能使用此地址:

Set MyEmail=CreateObject("CDO.Message")

MyEmail.Subject="Subject"
MyEmail.From="name@domain.com"
MyEmail.To="a@a.com"
MyEmail.TextBody="Testing one two three."

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2

'SMTP Server
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.server.com"

'SMTP Port
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25

MyEmail.Configuration.Fields.Update
MyEmail.Send

set MyEmail=nothing


如果您的SMTP服务器需要用户名和密码,则将这些行粘贴到MyEmail.Configuration.Fields.Update行上方:

'SMTP Auth (For Windows Auth set this to 2)
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
'Username
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="username"
'Password
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")="password"


有关使用CDO通过VBScript发送电子邮件的更多信息,请参见以下链接:
http://www.paulsadowski.com/wsh/cdo.htm

关于vbscript - VBScript在不运行Outlook的情况下发送电子邮件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7041938/

10-11 22:25
查看更多