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

标签 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

但是,这导调用子邮件无法发送,因为我的 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/

相关文章:

javascript - 如何将 VBScript 变量传递给 iMacros Javascript 宏?

reflection - 列出对象方法和属性

python - 像 PHP 一样用 Python 发送电子邮件

html - Div 样式在 Outlook 电子邮件中不起作用

database - VBScript:从 Scripting.Dictionary 中排序项目

ms-access - 通过 VBScript 从文本中删除尾随的不可见字符

php - linux群发邮件

java - 使用 Microsoft Graph 通过 Java Cron 作业发送动态电子邮件

c# - 当 outlook 的连接状态发生变化时触发的事件处理程序

outlook - 背景图像在 Outlook 2007 及更高版本中不起作用