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

75,942

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 "
Share:
75,942

Related videos on Youtube

user2480100
Author by

user2480100

Updated on September 18, 2022

Comments

  • user2480100
    user2480100 over 1 year

    I want to send a mail from a Windows batch file. Here is my code:

    blat test.log -server smtp.gmx.com -to [email protected] -f [email protected] -s "test mail" -u "[email protected]" -pw "password" -debug -log envois-mails.log -timestamp 
    

    Here is the error I get:

    2014.07.23 14:07:26 (Wed): <<<getline<<< 535 Authentication credentials invalid
    2014.07.23 14:07:26 (Wed): The SMTP server did not accept Auth PLAIN value.
    Are your login userid and password correct?
    2014.07.23 14:07:26 (Wed): >>>putline>>> AUTH LOGIN
    2014.07.23 14:07:26 (Wed): <<<getline<<< 334 VXNlcm5hbWU6
    2014.07.23 14:07:26 (Wed): >>>putline>>> b3Vzcy56YWltQGdtYWlsLmNvbQ==
    2014.07.23 14:07:26 (Wed): <<<getline<<< 334 UGFzc3dvcmQ6
    2014.07.23 14:07:26 (Wed): >>>putline>>> aG90bWFpbGhvdG1haWw=
    2014.07.23 14:07:26 (Wed): <<<getline<<< 535 Authentication credentials invalid
    2014.07.23 14:07:26 (Wed): The SMTP server did not accept Auth LOGIN PASSWD value.
    2014.07.23 14:07:26 (Wed): >>>putline>>> QUIT
    2014.07.23 14:07:26 (Wed): <<<getline<<< 221 gmx.com Service closing transmission channel
    

    Do you have you any idea about how to resolve the problem?

    I am using Blat to send the emails

    • Ramhound
      Ramhound almost 10 years
      It appears your authentication credentials are not valid based on the error. What are you using to actually send the emails beause you don't indicate that.
    • user2480100
      user2480100 almost 10 years
      i'm using blat, and i'm sure that authentication credentials are valid
    • Ramhound
      Ramhound almost 10 years
      The server allows SMTP connections. What restrictions does it place on those connections?
    • user2480100
      user2480100 almost 10 years
      honestly i do not know! I know nothing about network
    • Ramhound
      Ramhound almost 10 years
      If you don't know how can we answer this question? I suggest you find out.
    • 100rabh
      100rabh almost 10 years
      The SMTP server did not accept Auth PLAIN value guessing servers expects SSL/TLS connection
    • Gaurav Joseph
      Gaurav Joseph over 9 years
      @user2480100, Freunde I am sure that either your credentials or your SMTP server are wrong in absolute terms. How is it that you are logging into a gmx server using a gmail.com username????
  • user2480100
    user2480100 almost 10 years
    I can not switch to Powershell, because this sends of emails is just a small part of my script, do you another idea ?