sending mail from Batch file

WindowsCommand LineBatch FileEmail Client

Windows Problem Overview


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?

Windows Solutions


Solution 1 - Windows

Blat:

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

Solution 2 - Windows

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 = "[email protected]"
$objMailMessage.To.Add("[email protected]")
$objMailMessage.Subject = "eMail subject Notification"
$objMailMessage.Body = "Hello world!"

$smtp.send($objMailMessage)

Solution 3 - Windows

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

Solution 4 - Windows

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"

Solution 5 - Windows

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

Solution 6 - Windows

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser73628View Question on Stackoverflow
Solution 1 - WindowsColin PickardView Answer on Stackoverflow
Solution 2 - WindowsPhilibert PerusseView Answer on Stackoverflow
Solution 3 - WindowsHashbrownView Answer on Stackoverflow
Solution 4 - WindowsRossFabricantView Answer on Stackoverflow
Solution 5 - WindowsKengView Answer on Stackoverflow
Solution 6 - WindowsExpertsblog.infoView Answer on Stackoverflow