sending mail from Batch file

只谈情不闲聊 提交于 2019-11-27 17:58:14

Blat:

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

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)

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

RossFabricant

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"

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

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"
Philibert Perusse

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

Expertsblog.info

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