windows 从批处理文件发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/709635/
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
sending mail from Batch file
提问by user73628
We have a script to backup files. After the backup operation is over, we would like to send a report as an email notification to some of our email addresses.
我们有一个脚本来备份文件。备份操作结束后,我们希望将报告作为电子邮件通知发送到我们的一些电子邮件地址。
How could this be done?
这怎么可能?
回答by Colin Pickard
Blat:
布拉特:
blat -to [email protected] -server smtp.example.com -f [email protected] -subject "subject" -body "body"
回答by Philibert Perusse
You can also use a Power Shell script:
您还可以使用 Power Shell 脚本:
$smtp = new-object Net.Mail.SmtpClient("mail.example.com")
if( $Env:SmtpUseCredentials -eq "true" ) {
$credentials = new-object Net.NetworkCredential("username","password")
$smtp.Credentials = $credentials
}
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = "[email protected]"
$objMailMessage.To.Add("[email protected]")
$objMailMessage.Subject = "eMail subject Notification"
$objMailMessage.Body = "Hello world!"
$smtp.send($objMailMessage)
回答by Hashbrown
PowerShell comes with a built in command for it. So running directly from a .bat
file:
PowerShell 附带了一个内置命令。所以直接从.bat
文件运行:
powershell -ExecutionPolicy ByPass -Command Send-MailMessage ^
-SmtpServer server.address.name ^
-To [email protected] ^
-From [email protected] ^
-Subject Testing ^
-Body 123
NB-ExecutionPolicy ByPass
is only needed if you haven't set up permissions for running PS from CMD
NB-ExecutionPolicy ByPass
仅当您尚未设置从 CMD 运行 PS 的权限时才需要
Also for those looking to call it from within powershell, drop everything before -Command
[inclusive], and `
will be your escape character (not ^
)
同样对于那些希望从 powershell 中调用它的人,在-Command
[inclusive]之前删除所有内容,`
并将成为您的转义字符(不是^
)
回答by RossFabricant
bmail. Just install the EXE and run a line like this:
邮箱。只需安装 EXE 并运行如下一行:
bmail -s myMailServer -f [email protected] -t [email protected] -a "Production Release Performed"
回答by laurie
Easiest way is to use a third-party application as mentioned by others
最简单的方法是使用其他人提到的第三方应用程序
If that is not an option I wrote a simple sendmail utility using vbscript & CDO which I called from a batch script
如果这不是一个选项,我使用从批处理脚本调用的 vbscript & CDO 编写了一个简单的 sendmail 实用程序
See the examples here http://www.paulsadowski.com/WSH/cdo.htm
回答by Keng
We use blat to do this all the time in our environment. I use it as well to connect to Gmail with Stunnel. Here's the params to send a file
我们在我们的环境中一直使用 blat 来执行此操作。我也使用它通过Stunnel连接到 Gmail 。这是发送文件的参数
blat -to [email protected] -server smtp.example.com -f [email protected] -subject "subject" -body "body" -attach c:\temp\file.txt
Or you can put that file in as the body
或者您可以将该文件作为正文放入
blat c:\temp\file.txt -to [email protected] -server smtp.example.com -f [email protected] -subject "subject"
回答by Philibert Perusse
You can also use sendmail. I am using it in this subversion hook to send email notifications: post-commit hook
您也可以使用sendmail。我在这个 subversion hook 中使用它来发送电子邮件通知:post-commit hook
回答by Expertsblog.info
There are multiple methods for handling this problem.
有多种方法可以处理这个问题。
My advice is to use the powerful Windows freeware console application SendEmail.
我的建议是使用强大的 Windows 免费软件控制台应用程序SendEmail。
sendEmail.exe -f [email protected] -o message-file=body.txt -u subject message -t [email protected] -a attachment.zip -s smtp.gmail.com:446 -xu gmail.login -xp gmail.password