sending mail from Batch file

偶尔善良 提交于 2019-11-27 04:14:44

问题


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?


回答1:


Blat:

blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body"



回答2:


You can also use a Power Shell script:

$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)



回答3:


PowerShell comes with a built in command for it. So running directly from a .bat file:

powershell -ExecutionPolicy ByPass -Command Send-MailMessage ^
    -SmtpServer server.address.name ^
    -To someone@what.ever ^
    -From noreply@possibly.fake ^
    -Subject Testing ^
    -Body 123

NB -ExecutionPolicy ByPass is only needed if you haven't set up permissions for running PS from CMD

Also for those looking to call it from within powershell, drop everything before -Command [inclusive], and ` will be your escape character (not ^)




回答4:


bmail. Just install the EXE and run a line like this:

bmail -s myMailServer -f Sender@foo.com -t receiver@foo.com -a "Production Release Performed"



回答5:


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

See the examples here http://www.paulsadowski.com/WSH/cdo.htm




回答6:


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 -to user@example.com -server smtp.example.com -f batch_script@example.com -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 user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject"



回答7:


You can also use sendmail. I am using it in this subversion hook to send email notifications: post-commit hook




回答8:


There are multiple methods for handling this problem.

My advice is to use the powerful Windows freeware console application SendEmail.

sendEmail.exe -f sender.from@mail.com -o message-file=body.txt -u subject message -t to.email.address@mail.com -a attachment.zip -s smtp.gmail.com:446 -xu gmail.login -xp gmail.password


来源:https://stackoverflow.com/questions/709635/sending-mail-from-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!