Wednesday, June 27, 2012

Send Email from Windows using VBS and optional SSL Authentication

As I have posted before you can configure Windows Servers to send alerts when they detect errors (commonly logged in the Event Log). If your internal SMTP enforces authentication (I hope using SSL) then the scripts will need to be changed a little bit just to add support for it.

Rather than modifying previous posted scripts I am sharing this time a script that will just send an email from VBS. Optionally you can use authentication if you just supply the credentials:
'''''''''''''''''''''''''''''''''''''''''''''''
'
' c:\scripts\events\sendEmail.vbs
'
' @Author: Nestor Urquiza
'
'
' @Description: Sends an email
'
'
'
''''''''''''''''''''''''''''''''''''''''''''''''
'
' Functions
'
Sub usage
Wscript.Echo Wscript.ScriptName & " <host> <port> <from> <to> <subject> <body> [sslUser] [sslPassword]"
WScript.Quit
End Sub
Function IsBlank(Value)
IsBlank = False
If IsEmpty(Value) or IsNull(Value) Then
IsBlank = True
End If
End Function
'Constants
strComputer = "."
'System config
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'Parameters
If ( Wscript.Arguments.Count < 6 or Wscript.Arguments.Count = 7 ) Then
Call usage
End If
host = Wscript.Arguments(0)
port = Wscript.Arguments(1)
from = Wscript.Arguments(2)
strTo = Wscript.Arguments(3)
subject = Wscript.Arguments(4)
body = Wscript.Arguments(5)
If (Wscript.Arguments.Count >= 8) Then
sslUser = Wscript.Arguments(6)
sslPassword = Wscript.Arguments(7)
End If
'Prepare email
Set objEmail = CreateObject("CDO.Message")
objEmail.From = from
objEmail.To = strTo
objEmail.Subject = "[" & strComputerName & "]" & " " & subject
objEmail.Textbody = body
Set emailCfg = objEmail.Configuration
emailCfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailCfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = host
emailCfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = port
If ( Not isBlank(sslUser) ) Then
'Wscript.Echo "Using SSL authentication"
emailCfg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailCfg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailCfg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = sslUser
emailCfg.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = sslPassword
End If
objEmail.Configuration.Fields.Update
objEmail.Send
A couple of examples showing how to use it to send internal (sampleDomain) non SSL authenticated emails and externals SSL autheticated ones (gmail)
C:\>cscript c:\scripts\events\sendEmail.vbs smtp.company.com 25 corporateUser@company.com anotherCorporateUser@company.com "testing smtp from windows vbs" "Just a test"
C:\>cscript c:\scripts\events\sendEmail.vbs smtp.gmail.com 465 gmailUser@gmail.com corporateUser@company.com "testing smtp from windows vbs" "Just a test" gmailUser@gmail.com gmaiUserPassword

2 comments:

Mike Martin said...

Hey Nestor, I tried out your VBS sendmail script. You should comment out line 78, because it causes a compile time error.

this is what I got:
C:\Bin\sendmail.vbs(78, 1) Microsoft VBScript compilation error: Expected statement

Nestor Urquiza said...

Biff, Thanks for the heads up. I have moved the code to a gist which does not end up in error formatting.

Followers