VBScript无需运行Outlook即可发送电子邮件

use*_*354 15 vbscript sendmail outlook-2007

我已经编写了一个每晚运行的自动化测试,我想在测试结束后每晚通过电子邮件发送结果.

为了做到这一点,我试图将以下内容放在我的批处理文件的末尾:

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
Run Code Online (Sandbox Code Playgroud)

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

无论如何都要发送此电子邮件而不在机器上实际运行Outlook.

谢谢!

mic*_*386 28

您可以使用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
Run Code Online (Sandbox Code Playgroud)

如果您的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"
Run Code Online (Sandbox Code Playgroud)

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