Back to all How-tos

Send an email via a script

The following is a VBScript example that sends an email through an SSL Connection, using gmail’s SMTP server. This, of course, assumes that you have a gmail account!

The code provided below can be used in a Run Script Workflow task. Don’t forget to replace the following placeholder inside the script:

  • {login}: Enter your Gmail account email address (i.e. myaddress@gmail.com)
  • {password}: Enter the password associated with the Gmail account above
  • {emailFrom}: Enter the Gmail account email address you want to send the email from (normally the same as the {login} placeholder)
  • {emailTo}: Enter the email address you want to send the email to

Also, for the script to work, you must first login to your Gmail account and enable less secure apps by accessing the following page https://www.google.com/settings/security/lesssecureapps.

Option explicit
Dim MyMsg
Set MyMsg = CreateObject("CDO.Message")

''Change the values below to meet your own settings
''Port 25 is used for standard mail. Port 465 or 587 is used for SSL mail
SMTPConfig MyMsg, "smtp.gmail.com", 465, "{login}", "{password}", True


MyMsg.From = "{emailFrom}"
MyMsg.To = "{emailTo}"
MyMsg.Subject = "This is the subject of the email"
MyMsg.TextBody = "This is the body of the email."

' Uncomment following line if your want to attach
' the current job file to the email

'MyMsg.AddAttachment Watch.GetJobFileName

MyMsg.Send

Sub SMTPConfig(msg, srv, port, user, pw,useSSL)
Const cdoFieldPrefix = "http://schemas.microsoft.com/cdo/configuration/"
with msg
.Configuration.Fields.Item (cdoFieldPrefix & "sendusing" ) = 2
.Configuration.Fields.Item (cdoFieldPrefix & "smtpserver" ) = srv
.Configuration.Fields.Item (cdoFieldPrefix & "smtpserverport" ) = port
.Configuration.Fields.Item (cdoFieldPrefix & "sendusername" ) = user
.Configuration.Fields.Item (cdoFieldPrefix & "sendpassword" ) = pw
.Configuration.Fields.Item (cdoFieldPrefix & "smtpauthenticate") = 1
.Configuration.Fields.Item (cdoFieldPrefix & "smtpusessl") = useSSL
.Configuration.Fields.Update
end with
end sub

Leave a Reply

Your email address will not be published. Required fields are marked *