VBScript 无需运行 Outlook 即可发送电子邮件 [英] VBScript to send email without running Outlook
问题描述
我编写了一个每晚运行的自动化测试,我想在每晚测试完成后通过电子邮件发送结果.
I have written an automated test that runs each night, and I would like to email the results each night once the test is finished.
为了做到这一点,我尝试将以下内容放在批处理文件的末尾:
In order to do this I attempted to put the following at the end of my batchfile:
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.
However, this is causing the email to not send because my Outlook is not open, as the test is run in the background, and I have no access to the UI.
是否可以在不实际在机器上运行 Outlook 的情况下发送此电子邮件.
Is there anyway to send this email without actually running outlook on the machine.
谢谢!
推荐答案
您可以在 VBScript 中使用 CDO.Message 对象在没有 Outlook 的情况下发送电子邮件.您需要知道您的 SMTP 服务器的地址才能使用它:
You can send email without Outlook in VBScript using the CDO.Message object. You will need to know the address of your SMTP server to use this:
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
行上方:
If your SMTP server requires a username and password then paste these lines in above the MyEmail.Configuration.Fields.Update
line:
'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
More information on using CDO to send email with VBScript can be found on the link below: http://www.paulsadowski.com/wsh/cdo.htm
这篇关于VBScript 无需运行 Outlook 即可发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!