How to send a simple email from a Windows batch file?

307,764

Solution 1

Max is on he right track with the suggestion to use Windows Scripting for a way to do it without installing any additional executables on the machine. His code will work if you have the IIS SMTP service setup to forward outbound email using the "smart host" setting, or the machine also happens to be running Microsoft Exchange. Otherwise if this is not configured, you will find your emails just piling up in the message queue folder (\inetpub\mailroot\queue). So, unless you can configure this service, you also want to be able to specify the email server you want to use to send the message with. To do that, you can do something like this in your windows script file:

Set objMail = CreateObject("CDO.Message")
Set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.your-site-url.com" 'your smtp server domain or IP address goes here
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email
'uncomment next three lines if you need to use SMTP Authorization
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
objFlds.Update
objMail.Configuration = objConf
objMail.FromName = "Your Name"
objMail.From = "[email protected]"
objMail.To = "[email protected]"
objMail.Subject = "Email Subject Text"
objMail.TextBody = "The message of the email..."
objMail.Send
Set objFlds = Nothing
Set objConf = Nothing
Set objMail = Nothing

Solution 2

I've used Blat ( http://www.blat.net/ ) for many years. It's a simple command line utility that can send email from command line. It's free and opensource.

You can use command like "Blat myfile.txt -to [email protected] -server smtp.domain.com -port 6000"

Here is some other software you can try to send email from command line (I've never used them):
http://caspian.dotconf.net/menu/Software/SendEmail/
http://www.petri.co.il/sendmail.htm
http://www.petri.co.il/software/mailsend105.zip
http://retired.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm

Here ( http://www.petri.co.il/send_mail_from_script.htm ) you can find other various way of sending email from a VBS script, plus link to some of the mentioned software

The following VBScript code is taken from that page

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "[email protected]"
objEmail.To = "[email protected]"
objEmail.Subject = "Server is down!"
objEmail.Textbody = "Server100 is no longer accessible over the network."
objEmail.Send

Save the file as something.vbs

Set Msg = CreateObject("CDO.Message")

With Msg

 .To = "[email protected]"
 .From = "[email protected]"
 .Subject = "Hello"
 .TextBody = "Just wanted to say hi."
 .Send

End With

Save the file as something2.vbs

I think these VBS scripts use the windows default mail server, if present. I've not tested these scripts...

Solution 3

If PowerShell is available, the Send-MailMessage commandlet is a single one-line command that could easily be called from a batch file to handle email notifications. Below is a sample of the line you would include in your batch file to call the PowerShell script (the %xVariable% is a variable you might want to pass from your batch file to the PowerShell script):

--[BATCH FILE]--

:: ...your code here...
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -windowstyle hidden -command C:\MyScripts\EmailScript.ps1 %xVariable%

Below is an example of what you might include in your PowerShell script (you must include the PARAM line as the first non-remark line in your script if you included passing the %xVariable% from your batch file:

--[POWERSHELL SCRIPT]--

Param([String]$xVariable)
# ...your code here...
$smtp = "smtp.[emaildomain].com"
$to = "[Send to email address]"
$from = "[From email address]" 
$subject = "[Subject]" 
$body = "[Text you want to include----the <br> is a line feed: <br> <br>]"    
$body += "[This could be a second line of text]" + "<br> "

$attachment="[file name if you would like to include an attachment]"
send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Attachment $attachment -Priority high  
Share:
307,764
user448810
Author by

user448810

Programming Praxis provides a collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer.

Updated on September 23, 2021

Comments

  • user448810
    user448810 over 2 years

    I'm running Windows 2003 Service Pack 2. I have a batch file that runs on demand. I want to have an email sent every time the batch file runs. The email is simple, just a sentence indicating that the batch file ran; it is the same every time.

    I've tried a couple of things to get this done. I thought of telnet, but I can't figure out how to redirect a set of commands into telnet; Windows batch files don't have a Unix-style "here document," and calling "telnet <scriptfile" where scriptfile contains the commands to send an email didn't work. I also found a couple of solutions on the internet using CDO.Message, but I've never used that before and I kept getting error messages that I don't understand.

    How can I send a simple email from a Windows batch file?

  • user448810
    user448810 over 12 years
    I suspect I won't be allowed to put opensource software on the server in question. I'll try. Do you have any other suggestions?
  • user448810
    user448810 over 12 years
    SqlServer is installed on the machine. I'll look at that option. Thanks. In the meantime, other suggestions will still be gratefully accepted.
  • dbenham
    dbenham over 12 years
    @user448810 - Yea, I added VBScript to my answer. It could be done with JavaScript as well.
  • user448810
    user448810 over 12 years
    This works. Thank you! And thanks to everyone else who responded.
  • Impulss
    Impulss over 11 years
    Thanks a bunch! This worked a treat! @user448810 check and compile it yourself if the integrity of the software is in question. It doesn't require installation as such, just throw it into your path and use it as you need it.
  • miroxlav
    miroxlav over 6 years
    Easily readable and works great. In case of plain-text messages (without argument -BodyAsHtml) newline is denoted by `n or `r`n.
  • Sunil Chaudhary
    Sunil Chaudhary over 5 years
    'objFlds.Item' is not recognized as an internal or external command, operable program or batch file.
  • Lyes
    Lyes over 4 years
    at the top of your code you should add: $emailSmtpServer ="smtp.yourMailServer.com"
  • Priya
    Priya over 2 years
    Do I run this whole script on cmd prompt only? I have the same problem as @SunilChaudhary. I get the same error for all i.e., also for objMail.TextBody, objMail.Send, objMail.From and so on
  • Priya
    Priya over 2 years
    @dmarietta How did you run this? On cmd? Do I have something installed on PC to run this? I get the errors 'objMail.Configuration' is not recognized as an internal or external command, operable program or batch file. Also for all objFlds.Item, objMail.FromName, objMail.From, objMail.To, objMail.Subject and so on
  • dmarietta
    dmarietta over 2 years
    @Priya These would be run in a Windows Script file. Put them in a .VBS file to create a Windows Script file. These are not CommandLine commands. These run in windows script which is available on all versions of Windows OS back to Windows 2000.
  • Priya
    Priya over 2 years
    @dmarietta Thank you! I now could run the script by saving in .vbs. But I did not get an E-Mail even after I ran the script :(
  • Priya
    Priya over 2 years
    @dmarietta Hey Thanks a lot. It worked now. i actually had two smtp server names and that was there the problem was. I now solved in and am able to get mails :) Thanks for your answer. Btw, can I also add CC/BCC in this? I tried "objMail.Cc= " but server rejected saying too many recipients even though I just added only one mail in Cc
  • dmarietta
    dmarietta over 2 years
    @Priya Yes, the CDO.Message object, does have .CC and .BCC properties for sending to multiple recipients. It should work fine. If it is being rejected, it is possible that the SMTP server you are forwarding through is what is rejecting the message. There may be a rule that needs to be modified there.