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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''''''''''''''''''''''''''''''''''''''''''''''' | |
' | |
' 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 |
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:
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
Biff, Thanks for the heads up. I have moved the code to a gist which does not end up in error formatting.
Post a Comment