我们有一个备份文件的脚本.备份操作结束后,我们希望将报告作为电子邮件通知发送到我们的某些电子邮件地址.
怎么可以这样做?
布拉特:
blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body"
您还可以使用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 = "script@mycompany.com" $objMailMessage.To.Add("you@yourcompany.com") $objMailMessage.Subject = "eMail subject Notification" $objMailMessage.Body = "Hello world!" $smtp.send($objMailMessage)
PowerShell附带内置命令.所以直接从.bat
文件运行:
powershell -ExecutionPolicy ByPass -Command Send-MailMessage ^
-SmtpServer server.address.name ^
-To someone@what.ever ^
-From noreply@possibly.fake ^
-Subject Testing ^
-Body 123
-ExecutionPolicy ByPass
仅当您尚未设置从CMD运行PS的权限时才需要NB
对于那些希望在PowerShell中调用它的人,在-Command
[包含] 之前删除所有内容,并且`
将成为你的转义字符(不是^
)
bmail.只需安装EXE并运行如下所示的行:
bmail -s myMailServer -f Sender@foo.com -t receiver@foo.com -a "Production Release Performed"
最简单的方法是使用其他人提到的第三方应用程序
如果这不是一个选项,我使用vbscript和CDO编写了一个简单的sendmail实用程序,我从批处理脚本调用它
请参阅http://www.paulsadowski.com/WSH/cdo.htm中的示例