Send email from VBScript using SMTP and Gmail

1 minute read

Today I wanted to send an email using SMTP from my gmail account. I searched the web and I found this and this and this which helped me a lot. So this is the vbscript:

on error resume next

Const schema   = "http://schemas.microsoft.com/cdo/configuration/"
Const cdoBasic = 1
Const cdoSendUsingPort = 2
Dim oMsg, oConf

' E-mail properties
Set oMsg      = CreateObject("CDO.Message")
oMsg.From     = "[email protected]"  ' or "Sender Name <[email protected]>"
oMsg.To       = "[email protected]"    ' or "Recipient Name <[email protected]>"
oMsg.Subject  = "Test from VBScript"
oMsg.TextBody = "If you can read this, the script worked!"

' GMail SMTP server configuration and authentication info
Set oConf = oMsg.Configuration
oConf.Fields(schema & "smtpserver")       = "smtp.gmail.com" 'server address
oConf.Fields(schema & "smtpserverport")   = 465              'port number
oConf.Fields(schema & "sendusing")        = cdoSendUsingPort
oConf.Fields(schema & "smtpauthenticate") = cdoBasic         'authentication type
oConf.Fields(schema & "smtpusessl")       = True             'use SSL encryption
oConf.Fields(schema & "sendusername")     = "[email protected]" 'sender username
oConf.Fields(schema & "sendpassword")     = "passwordi"      'sender password
oConf.Fields.Update()

' send message
oMsg.Send()

' Return status message
If Err Then
	resultMessage = "ERROR " & Err.Number & ": " & Err.Description
	Err.Clear()
Else
	resultMessage = "Message sent ok"
End If

Wscript.echo(resultMessage)

Comments