我编写了一个每晚运行的自动化测试,我想在测试完成后每晚通过电子邮件发送结果。
为了做到这一点,我试图将以下内容放在我的批处理文件的末尾:
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 无需运行 Outlook 即可发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7041938/