How to send mail from a Windows batch file using Blat utility

Solution 1:

I think you can easily get it done through Powershell.

Follow these Steps:

Step 1 - Open CMD (Run as Administrator)
Step 2 - Type Powershell (Hit Enter)
Step 3 - Copy the below code in notepad first

$EmailFrom = “Your email Address” $EmailTo = “Recipients email Address”
$Subject = “The subject of your email”
$Body = “This is just a test mail to verify the working of CMD”
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“username”, “password”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Step 4 - change Your Email address to sender's email
Step 5 - Change Recipients Email Address
Step 6 - Replace Subject and body text according to your need
Step 7 - Replace "username" and "password" with your credentials.
Step 8 - Copy the above code and paste in windows Powershell.

This will surely work for gmail. For others you may try changing the SMTP server and client details.

Solution 2:

As others have guessed, this is because of Gmail requiring TLS/SSL connection and from what I can see Blat does not support that.

I worked around it with stunnel which works really well. It sets up a TLS/SSL tunnel (to gmail SMTP in this case) which non-TLS/SSL enabled apps can use to send emails through. I also use it to email (via gmail) from a couple of other apps that don't support TLS/SSL. TBH I don't recall the config but it was pretty straight forward.

You still use your gmail credentials, but substitute your stunnel server (localhost? - I have it set up on it's own Linux server and use it's IP but it's cross platform so I guess it could use localhost on Windows).

Solution 3:

It works for me, by using double quotes around variables.

I am using batch script to call powershell Send-MailMessage

Batch Script:send_email.bat

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -windowstyle hidden -command 'E:\path\send_email.ps1

Pwershell Script send_email.ps1

Send-MailMessage -From "noreply@$env:computername" -To '<[email protected]>' -Subject 'Blah Blah' -SmtpServer  'smtp.domain.com'  -Attachments 'E:\path\file.log' -BODY "Blah Blah on Host: $env:computername "