从 Windows 脚本发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/152323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:26:30  来源:igfitidea点击:

Send mail from a Windows script

windowsemailscriptingsmtpwsh

提问by J?rgen Lundberg

I would like to send mail from a script on a Windows Server 2003 Standard Edition. I think the server setup is pretty much out of the box.

我想从 Windows Server 2003 标准版上的脚本发送邮件。我认为服务器设置几乎是开箱即用的。

The mail server is an Exchange one, and when you're on the internal network you can use plain old SMTP. I have done it from my machine with Perl, but unfortunately Perl is not available on the server.

邮件服务器是一个 Exchange 服务器,当您在内部网络上时,您可以使用普通的旧 SMTP。我已经用 Perl 在我的机器上完成了它,但不幸的是 Perl 在服务器上不可用。

Is there an easy way of doing this from a .bat-file or any other way that doesn't require installing some additional software?

有没有一种简单的方法可以从 .bat 文件或任何其他不需要安装一些额外软件的方式来做到这一点?

Edit:
Thanks for the quick replies. The "blat" thingie would probably work fine but with wscript I don't have to use a separate binary.

Edit:
感谢您的快速回复。“blat”东西可能会正常工作,但使用 wscript 我不必使用单独的二进制文件。

I didn't see PhiLho's post the first time I edited and selected an answer. No need for me to duplicate the code here.

我第一次编辑和选择答案时没有看到 PhiLho 的帖子。我不需要在这里复制代码。

Just save the script to a file, say sendmail.vbs, and then call it from the command prompt like so:
wscript sendmail.vbs

只需将脚本保存到一个文件中,比如 sendmail.vbs,然后从命令提示符调用它,如下所示:
wscript sendmail.vbs

采纳答案by PhiLho

It is possible with Wscript, using CDO:

可以使用 Wscript,使用 CDO:

Dim objMail

Set objMail = CreateObject("CDO.Message")

objMail.From = "Me <[email protected]>"
objMail.To = "You <[email protected]>"
objMail.Subject = "That's a mail"
objMail.Textbody = "Hello World"
objMail.AddAttachment "C:\someFile.ext"

---8<----- You don't need this part if you have an active Outlook [Express] account -----
' Use an SMTP server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

' Name or IP of Remote SMTP Server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
    "smtp.server.com"

' Server port (typically 25)
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

objMail.Configuration.Fields.Update
----- End of SMTP usage ----->8---

objMail.Send

Set objMail=Nothing
Wscript.Quit

Update: found more info there: VBScript To Send Email Using CDOBy default it seems it uses Outlook [Express], so it didn't worked on my computer but you can use a given SMTP server, which worked fine for me.

更新:在那里找到更多信息:使用 CDO 发送电子邮件的 VBScript默认情况下,它似乎使用 Outlook [Express],因此它在我的计算机上不起作用,但您可以使用给定的 SMTP 服务器,这对我来说效果很好。

回答by Jacek Szymański

I don't know if dropping a binary alongside the .bat file counts as installing software, but, if not, you can use blatto do this.

我不知道在 .bat 文件旁边放置二进制文件是否算作安装软件,但如果不是,您可以使用blat来执行此操作。

回答by Nathan Hartley

If the server happened (I realize how old this question is) to have Powershell v2 installed, the CmdLet Send-MailMessage would do this in one line.

如果服务器碰巧(我意识到这个问题有多老)安装了 Powershell v2,则 CmdLet Send-MailMessage 将在一行中执行此操作。

Send-MailMessage [-To] <string[]> [-Subject] <string> -From <string> [[-Body] <string>] [[-SmtpServer] <string>] [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Cc <string[]>] [-Credential <PSCredential>] [-DeliveryNotficationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Priority {Normal | Low | High}] [-UseSsl] [<CommonParameters>]

回答by Re0sless

If you have outlook/exchange installed you should be able to use CDONTs, just create a mail.vbs file and call it in a batch file like so (amusing they are in the same dir)

如果您安装了 Outlook/Exchange,您应该可以使用 CDONT,只需创建一个 mail.vbs 文件并在批处理文件中调用它,就像这样(有趣的是它们在同一个目录中)

wscript mail.vbs

for the VBScript code check out

对于 VBScript 代码,请查看

http://support.microsoft.com/kb/197920

http://support.microsoft.com/kb/197920

http://www.w3schools.com/asp/asp_send_email.asp

http://www.w3schools.com/asp/asp_send_email.asp

forget the fact they the two links speak about ASP, it should work fine as a stand alone script with out iis.

忘记他们这两个链接谈论 ASP 的事实,它应该可以作为没有 iis 的独立脚本正常工作。

回答by vacpro

Use CDONTS with Windows Scripting Host (WScript)

将 CDONTS 与 Windows 脚本宿主 (WScript) 一起使用

回答by Moshe

I think that you'll have to install some ActiveX or other component what could be invoked from WScript, such as: http://www.activexperts.com/ActivEmail/and: http://www.emailarchitect.net/webapp/SMTPCOM/developers/scripting.asp

我认为您必须安装一些可以从 WScript 调用的 ActiveX 或其他组件,例如:http: //www.activexperts.com/ActivEmail/ 和:http: //www.emailarchitect.net/webapp/ SMTPCOM/developers/scripting.asp

Otherwise, you'll have to write the entire SMTP logic (if possible, not sure) in WScript all on your own.

否则,您必须自己在 WScript 中编写整个 SMTP 逻辑(如果可能,不确定)。

回答by DssTrainer

Is there a way you send without referencing the outside schema urls. http://schemas.microsoft.com/cdo/configuration/

有没有一种方法可以在不引用外部架构 url 的情况下发送。 http://schemas.microsoft.com/cdo/configuration/

That is highly useless as it can't be assumed all boxes will have outside internet access to send mail internally on the local exchange. Is there a way to save the info from those urls locally?

这是非常无用的,因为不能假设所有盒子都有外部互联网访问权限以在本地交换机内部发送邮件。有没有办法在本地保存这些网址中的信息?