windows 如何从 DOS 批处理命令发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3290152/
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
How do I send an email from a DOS batch command?
提问by Honus Wagner
I have a batch file in DOS that does some checking and I need to fire off an email when its done. I've found a few solutions on the interwebz but most of them are 3rd party or just simply open up a new message in Outlook. I need the command to send an email in its entirety without any human interaction.
我在 DOS 中有一个批处理文件,可以进行一些检查,完成后我需要发送一封电子邮件。我在interwebz 上找到了一些解决方案,但其中大多数是第3 方的,或者只是在Outlook 中打开一条新消息。我需要命令来发送完整的电子邮件,而无需任何人工交互。
We use MS Exchange here, if that matters.
如果这很重要,我们在这里使用 MS Exchange。
Thanks!
谢谢!
采纳答案by Hans Olsson
Assuming:
假设:
- Your Exchange server accepts emails via SMTP on port 25.
- You want to send a simple text only email without attachments.
- It's possible to drive telnet (or a similar telnet client) from a batch file.
- 您的 Exchange 服务器通过端口 25 上的 SMTP 接受电子邮件。
- 您想发送不带附件的纯文本电子邮件。
- 可以从批处理文件驱动 telnet(或类似的 telnet 客户端)。
You could just send a simple email via Telnet. This link shows an example of how to do it: http://www.yuki-onna.co.uk/email/smtp.html
您可以通过 Telnet 发送简单的电子邮件。此链接显示了如何操作的示例:http: //www.yuki-onna.co.uk/email/smtp.html
If assumption 2 or 3 is wrong, you could write a command line SMTP client for sending simples emails fairly easily in many languages and then call it from your batch file.
如果假设 2 或 3 是错误的,您可以编写一个命令行 SMTP 客户端,以相当容易地以多种语言发送简单的电子邮件,然后从您的批处理文件中调用它。
回答by PowerStat
Try
尝试
http://sourceforge.net/projects/blat/
http://sourceforge.net/projects/blat/
it's an open source mailer from SourceForge with a lot of options.
它是来自 SourceForge 的开源邮件程序,有很多选项。
回答by user697473
Try http://caspian.dotconf.net/menu/Software/SendEmail/.
试试http://caspian.dotconf.net/menu/Software/SendEmail/。
It's a third-party utility, but you can easily call it from a batch file. I do. I have the same need that you do: send e-mail at the end of a batch file with no human interaction.
它是第三方实用程序,但您可以轻松地从批处理文件中调用它。我愿意。我和你有同样的需求:在没有人为交互的批处理文件末尾发送电子邮件。
回答by storsoc
If your host OS has PowerShell, you can accomplish this from CMD.EXE as a one-liner (effectively) without also incurring script-execution privilege blockingby simply doing:
如果您的主机操作系统具有 PowerShell,您可以从 CMD.EXE 作为单行(有效)完成此操作,而无需通过简单地执行以下操作而导致脚本执行权限阻塞:
powershell -command $S=New-Object Net.Mail.SmtpClient('your.smtp.server',port);$S.Send('addr-from','addr-to','subject','body')
Concatenating the lines with ; and executing as a single PS command dodges checking/setting script execution. There is, of course, a limitationto the length of a single command string in CMD.EXE so this exact technique puts some limitations on the number of recipients, size of subject and body, etc. but there are ways around this, covered elsewhere, I'm sure.
用 ; 连接行 并作为单个 PS 命令执行可避免检查/设置脚本执行。当然,CMD.EXE 中的单个命令字符串的长度是有限制的,所以这种精确的技术对接收者的数量、主题和正文的大小等有一些限制,但有一些方法可以解决这个问题,其他地方有介绍, 我确定。
Naturally, my usage of that is much more robust, being wrapped in a .BAT that turns it into a utility (perfect for low-friction scripts on locked-down Windows hosts) with parameter checking, error handling, logging, etc. Go nuts.
自然,我对它的使用更加健壮,被包装在一个 .BAT 中,将它变成一个实用程序(非常适合锁定的 Windows 主机上的低摩擦脚本),具有参数检查、错误处理、日志记录等功能。 .
If your SMTP server requires authentication, you can add the additional necessary configuration of the SmtpClientas needed, which will of course further constrain length.
如果您的 SMTP 服务器需要身份验证,您可以根据需要添加SmtpClient的额外必要配置,这当然会进一步限制长度。
EDIT: Insert this before the $S.Send
to add authentication:
编辑:在$S.Send
添加身份验证之前插入:
$S.EnableSsl=$true;$S.Credentials=New-Object System.Net.NetworkCredential('usr', 'pass');
回答by Rahul
powershell -command $S=New-Object Net.Mail.SmtpClient('your.smtp.server',port);$S.Send('addr-from','addr-to','subject','body')`
this is working for me
这对我有用
but I have to send an email with attachment to multiple recepients
但我必须向多个收件人发送带有附件的电子邮件