How can we help?

AxEmail.Smtp


Introduction

Skip to properties and methods

The Smtp object implements the SMTP (Simple Mail Transfer Protocol). The SMTP protocol is the de facto standard protocol for sending email through on the internet.

This object can be used to connect to a remote SMTP server and send out one or more email messages.

Send email through SMTP

Send an email through the SMTP protocol

Set objSmtp = CreateObject("AxEmail.Smtp")              ' Create the SMTP Protocol object
Set objMail = CreateObject("AxEmail.Message")           ' Create the email message object

Wscript.Echo "Auron Email Component Version"  & objSmtp.Version & "; Build"  & _
  objSmtp.Build & "; Module"  & objSmtp.Module
WScript.Echo "License Status:"  & objSmtp.LicenseStatus & vbCrLf

objSmtp.LogFile = "log.txt"                             ' Keep a session log

' Open a connection to the SMTP server
objSmtp.SetSecure
objSmtp.Connect" smtp.gmail.com", "me@gmail.com", "password"
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If

' Compose an email message
objMail.FromAddress = "me@gmail.com"
objMail.FromName = "My Name"
objMail.Subject = "Hi !"
objMail.BodyPlainText = "Hello, How are you doing ?"
objMail.BodyHtml = "<html><body><h2>Hello</h2><p>How are you doing ?</p></body></html>"
objMail.AddTo "someone@example.com", "Someone"

' Send the email
objSmtp.Send objMail
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)    
End If

' Disconnect from the server
objSmtp.Disconnect
WScript.Echo "Done !"
How to run this example

Properties and Methods

Property Type Read/Write Description
Version String Read Version number of the Auron Email Component
Build String Read Build number of the Auron Email Component
Module String Read Module name of the Auron Email Component
LicenseStatus String Read License Status
LicenseKey String Read/Write License Key
LastError Number Read Result of the last called method
LogFile String Read/Write The path to a logfile which can be used for troubleshooting
HostPort Number Read/Write The host port of the SMTP server to connect to
Authentication Number Read/Write The SMTP authentication method
BearerToken String Read/Write OAuth2 bearer token
UseStartTls Boolean Read/Write Use a secure connection if possible
LastSmtpResponse String Read Last SMTP response from the server
TimeoutConnect Number Read/Write Timeout (msecs) for a connection to be established
TimeoutAuthentication Number Read/Write Timeout (msecs) to authenticate
TimeoutCommand Number Read/Write Timeout (msecs) for each SMTP command to complete
TimeoutData Number Read/Write Timeout (msecs) for the SMTP DATA command to complete

Method Description
Clear Reset all properties to their default values
SaveLicenseKey Save the License Key in the registry
GetErrorDescription Get the description of the given error
Sleep Sleep for the specified number of milliseconds
SetSecure Enables TLS/SSL and sets the correct port number
Connect Connect to the SMTP server
Disconnect Disconnect from the SMTP server
Send Send an email message

Version property

Return the version number of the Auron Email Component

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Wscript.Echo "Auron Email Component Version"  & objSmtp.Version & "; Build"  & _
  objSmtp.Build & "; Module"  & objSmtp.Module
WScript.Echo "License Status:"  & objSmtp.LicenseStatus & vbCrLf
...

Build property

Return the build number of the Auron Email Component.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Wscript.Echo "Auron Email Component Version"  & objSmtp.Version & "; Build"  & _
  objSmtp.Build & "; Module"  & objSmtp.Module
WScript.Echo "License Status:"  & objSmtp.LicenseStatus & vbCrLf
...

Module property

Return the module name of the Auron Email Component.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Wscript.Echo "Auron Email Component Version"  & objSmtp.Version & "; Build"  & _
  objSmtp.Build & "; Module"  & objSmtp.Module
WScript.Echo "License Status:"  & objSmtp.LicenseStatus & vbCrLf
...

LicenseStatus property

The status of your license. In case you have not licensed the product, the property holds the trial expiration date.
For details, see Product Activation.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")                 ' Create new instance
WScript.Echo "License Status:"  & objSmtp.LicenseStatus
WScript.Echo "License Key:"  & objSmtp.LicenseKey

LicenseKey property

A license key is required to unlock this component after the trial period has expired. Assign the LicenseKey property every time a new instance of this component is created (see code below). Alternatively, the LicenseKey property can be set automatically. This requires the license key to be stored in the registry. For details, see Product Activation.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")                 ' Create new instance
objSmtp.LicenseKey = "XXXXX-XXXXX-XXXXX"                ' Assign your license key 
WScript.Echo "LicenseKey:"  & objSmtp.LicenseKey

LastError property

Completion code of the last called function. To find the error description of a given error code, go to the online error codes page.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objMail = CreateObject("AxEmail.Message")
...  
' Compose an email message
objMail.Subject = "Hi !"
objMail.BodyPlainText = "Hello, How are you doing ?"
objMail.BodyHtml = "<html><body><h2>Hello</h2><p>How are you doing ?</p></body></html>"
objMail.AddTo "someone@example.com", "Someone"
...
' Send the email
objSmtp.Send objMail
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)    
End If
...

LogFile property

By default, LogFile holds an empty string and nothing is logged. If a valid file name is assigned to it, the Auron Email Component will write debug information to this file. Output data is written at the end of the file, the file is not cleared.

By default the log file uses the UTF16LE character encoding. To use UTF8 please prefix the log file name with the text: ‘UTF8:’ (e.g.: ‘UTF8:logfile.txt’).

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
objSmtp.LogFile = "C:\temp\log.txt"
objSmtp.Connect "smtp.example.com", "me@example.com", "password"
...

HostPort property

This is the host port of the SMTP server to connect to. Set this property if the port is different from the standard SMTP port (25).

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objMail = CreateObject("AxEmail.Message")
...
objSmtp.HostPort = 8025
objSmtp.Connect "smtp.example.com", "me@example.com", "password"
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

Authentication property

This is the authentication method that should be used. By default the preferred authentication method will be automatically detected.

Use one of these constants.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objConst = CreateObject("AxEmail.Constants")
Set objMail = CreateObject("AxEmail.Message")
...
objSmtp.Authentication = objConst.SMTP_AUTH_MD5CRAM
objSmtp.Connect "smtp.example.com", "me@example.com", "password"
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

BearerToken property

Set the bearer token to use OAuth2 authentication. If the bearer token is set then the user password can be left blank.

Obtain the bearer token by using the OAuth2 object.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objConst = CreateObject("AxEmail.Constants")
Set objMail = CreateObject("AxEmail.Message")
...
objPop3.SetSecure
objSmtp.Authentication = objConst.SMTP_AUTH_OAUTH2
objPop3.BearerToken = "12345678901234567890"
objSmtp.Connect "smtp.example.com", "me@example.com"
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

UseStartTls property

Use a secure connection if supported by the SMTP server.
If the property is set to True, Connect will start a secure connection if the SMTP server supports it.
If the property is set to False, Connect will NOT start a secure connection, even in case the SMTP does support it.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objConst = CreateObject("AxEmail.Constants")
...
Set objSmtp.UseStartTls = False
objSmtp.Connect "smtp.example.com", "me@example.com", "password"
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

LastSmtpResponse property

The last SMTP response from the server

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objConst = CreateObject("AxEmail.Constants")
Set objMail = CreateObject("AxEmail.Message")
...  
objSmtp.Connect "smtp.example.com", "me@example.com", "password"
WScript.Echo "Server response:"  & objSmtp.LastSmtpResponse
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

TimeoutConnect property

Timeout (in milliseconds) for a connection to be established.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
...  
objSmtp.TimeoutConnect = 10000
objSmtp.Connect "smtp.example.com"," ","" 
WScript.Echo "Server response: " & objSmtp.LastSmtpResponse
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

TimeoutAuthentication property

Timeout (in milliseconds) to authenticate to the SMTP mail server.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
...  
objSmtp.TimeoutAuthentication = 10000  
objSmtp.Connect "smtp.example.com", "me@example.com", "password"
WScript.Echo "Server response:"  & objSmtp.LastSmtpResponse
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

TimeoutCommand property

Timeout (in milliseconds) for each SMTP command to complete. This applies to all SMTP commands (e.g.: AUTH, RCPT TO, QUIT) except the DATA command.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
...  
objSmtp.Connect "smtp.example.com", "me@example.com", "password"
... 
objSmtp.TimeoutCommand = 5000  
objSmtp.Send( objMail )
...

TimeoutData property

Timeout (msecs) for the SMTP DATA command to complete.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
...  
objSmtp.Connect "smtp.example.com", "me@example.com", "password"
...  
objSmtp.TimeoutData = 120000   
objSmtp.Send( objMail )
...

Clear method

This function resets all properties to their default values.

Parameters:

  • None

Return value:

Always 0.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")   
....
objSmtp.Clear
...

SaveLicenseKey method

Description:

Use SaveLicenseKey to store the license key permanently in the registry. When a license key is saved, it is restored automatically every time a new instance of the object is created. It is not recommended to save the license key if you distribute the component with your own software, because the key can be easily used by others.

Parameters:

  • None.

Return value:

Always 0. Check LastError property to see if the method was completed successfully.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")                 ' Create new instance
objSmtp.LicenseKey = "XXXXX-XXXXX-XXXXX"
objSmtp.SaveLicenseKey                                     ' Save license key to registry

For more information, see Product Activation.

GetErrorDescription method

GetErrorDescription provides the error description of a given error code.

Parameters:

  • The error code

Return value:

The error string.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
...
WScript.Echo "result:"  & objSmtp.LastError & ","  & _
  objSmtp.GetErrorDescription(objSmtp.LastError)

Sleep method

This function suspends the current thread for the specified number of milliseconds.

Parameters:

  • Milliseconds to sleep

Return value:

Always 0.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
....
objSmtp.Sleep 1000
...

SetSecure method

This function makes sure TLS/SSL is enabled and the port number to connect on is set correctly.

Parameters:

  • (Optional) Port number, default: 465

Return value:

Always 0.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objMail = CreateObject("AxEmail.Message")
...  
objSmtp.SetSecure
objSmtp.Connect "smtp.gmail.com", "me@gmail.com", "password"
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

Connect method

This function connects to the specified SMTP server

Parameters:

  • Hostname
  • Account name
  • Password

Return value:

Always 0.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objMail = CreateObject("AxEmail.Message")
...  
objSmtp.Connect "smtp.gmail.com", "me@gmail.com", "password"
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...

Disconnect method

This function disconnects from the SMTP server

Parameters:

  • None

Return value:

Always 0.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objMail = CreateObject("AxEmail.Message")
...  
objSmtp.Connect "smtp.gmail.com", "me@gmail.com", "password"
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)  
  WScript.Quit
End If
...
objSmtp.Disconnect

Send method

This functions sends the specified email.

Parameters:

  • EMail object

Return value:

Always 0.

Example:

Set objSmtp = CreateObject("AxEmail.Smtp")
Set objMail = CreateObject("AxEmail.Message")
...
objMail.BodyPlainText = "Hello, How are you doing ?"
objMail.BodyHtml = "<html><body><h2>Hello</h2><p>How are you doing ?</p></body></html>"
objMail.AddTo "someone@example.com", "Someone"
...
objSmtp.Send objMail
If objSmtp.LastError <> 0 Then
  WScript.Echo "Error:"  & objSmtp.GetErrorDescription(objSmtp.LastError)    
End If
...