In my never ending desire to use Batch files to do just about anything, I was recently needed batch file to send an email to users once task is completed with “Success” or “Failure” results. And since I was already using batch files to do number of things, I thought it wouldn’t harm if it can do one more thing …. send email to users !! Actually it is not possible to send email just from plain DOS commands so I added a bit flavor to it with use of good ‘ol VB Script. This distant cousin of batch script has always been popular among system admins, hackers and various other titles you can think of, due to its ability to access system level resources and more close ties to standard programing language VB.

I found over Google that most people suggest to use “CDO.Message” object to send email in VB Script. But there were not any “correct” script which solved my problem, it is because most of those scripts were required me to install SMPT server in my machine. But finally I was able to find a script which was doing exactly what it suppose to do (I honestly don’t remember which site was that because I had checked number of forums).

Finally working script is (with minor modification to original script),

Const fromEmail    = "Sender Email"
Const password    = "Email Login"
Const toEmail    = "Receiver(s) Email"
Const mailSubject = "Mail Subject"
Const mailBody = "Mail Body"

Dim emailObj, emailConfig
Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = fromEmail
emailObj.To       = toEmail
emailObj.Subject  = mailSubject
emailObj.TextBody = mailBody

Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver")       = "smtp server (google, yahoo, any one) "
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport")   = 465 'SSL Port
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")        = 2  'SMTP on N/W
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  'Basic Auth
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")       = true 'For SSL
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")     = fromEmail
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")     = password
emailConfig.Fields.Update

emailObj.Send

And that’s it. Just copy this code to a text file and save it as “.vbs” and use it to send email users using batch file.

That’s it for now …

It’s Just A Thought … Peace

Gaurang Sign

Random Posts:


Similar Posts: