VBScript 在不运行 Outlook 的情况下发送电子邮件

发布于 2024-11-29 10:17:27 字数 562 浏览 6 评论 0原文

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

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

Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0)
With MyItem
    .To = "[email protected]"
    .Subject = "Subject"
    .ReadReceiptRequested = False
    .HTMLBody = "resport"
End With
MyItem.Send

但是,这会导致电子邮件无法发送,因为我的 Outlook 未打开,因为测试在后台运行,并且我无权访问用户界面。

无论如何,是否可以在不实际在计算机上运行 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 = "[email protected]"
    .Subject = "Subject"
    .ReadReceiptRequested = False
    .HTMLBody = "resport"
End With
MyItem.Send

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.

Is there anyway to send this email without actually running outlook on the machine.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的痛♀有谁懂 2024-12-06 10:17:27

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

Set MyEmail=CreateObject("CDO.Message")

MyEmail.Subject="Subject"
MyEmail.From="[email protected]"
MyEmail.To="[email protected]"
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

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="[email protected]"
MyEmail.To="[email protected]"
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

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"

More information on using CDO to send email with VBScript can be found on the link below:
http://www.paulsadowski.com/wsh/cdo.htm

烂人 2024-12-06 10:17:27

这是您从其他服务(例如私人电子邮件)发送电子邮件的方式

Set objMessage = CreateObject("CDO.Message") 
 objMessage.Subject = "Wooow" 
 objMessage.From = "[email protected]" 
 objMessage.To = "[email protected]" 
 objMessage.TextBody = "You are awesome"
 Set objConfig = objMessage.Configuration
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "serverXXXXXX.web-hosting.com"
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YOUR_PASSWORD_XXXX"
 'Server port (typically 25)
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 4XX
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
 objConfig.Fields.Update
 objMessage.Send

我测试了它,它有效。

This is how you can send emails from other services like private email

Set objMessage = CreateObject("CDO.Message") 
 objMessage.Subject = "Wooow" 
 objMessage.From = "[email protected]" 
 objMessage.To = "[email protected]" 
 objMessage.TextBody = "You are awesome"
 Set objConfig = objMessage.Configuration
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "serverXXXXXX.web-hosting.com"
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YOUR_PASSWORD_XXXX"
 'Server port (typically 25)
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 4XX
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
 objConfig.Fields.Update
 objMessage.Send

I tested it, it's working.

﹎☆浅夏丿初晴 2024-12-06 10:17:27

是的。 Blat 或任何其他独立的 SMTP 邮件程序。 Blat 是一个功能相当齐全的 SMTP 客户端,从命令行运行

Blat 在这里

Yes. Blat or any other self contained SMTP mailer. Blat is a fairly full featured SMTP client that runs from command line

Blat is here

也只是曾经 2024-12-06 10:17:27

以下是架构文档的当前链接:

https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2003/ms873029(v=exchg.65)

而不是使用对象引用再说一次,一个漂亮的 With 语句看起来会更好:

Set Msg = CreateObject("CDO.Message")
With Msg
 .To = "[email protected]"
 .From = "[email protected]"
 .Subject = "Subject goes here."
 .TextBody = "Message goes here."
End With
With Msg.Configuration
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.example.com"
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = false
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 15
 .Fields.Update
End With
Msg.Send

关于 smtpusessl 字段的额外说明:这仅适用于 TLS 类型端口。它与 STARTTLS 协议不同。使用 STARTTLS 时只需将其设置为 false 即可。

Here is a current link to the schema documentation:

https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2003/ms873029(v=exchg.65)

Instead of using the object references over and over again, a nice With statement will look better:

Set Msg = CreateObject("CDO.Message")
With Msg
 .To = "[email protected]"
 .From = "[email protected]"
 .Subject = "Subject goes here."
 .TextBody = "Message goes here."
End With
With Msg.Configuration
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.example.com"
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = false
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 15
 .Fields.Update
End With
Msg.Send

Extra note about the smtpusessl field: This works on a TLS type port only. It is not the same as the STARTTLS protocol. Simply set it to false when using STARTTLS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文