Sample code for 30+ languages & platforms
AutoIt

SMTP XOAUTH2 Authentication

See more SMTP Examples

Demonstrates how to authenticate using an OAuth2 access token. This example assumes you have already obtained the access token and now wish to use it in an SMTP session.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

Local $sOauth2_access_token = "some_previously_obtained_token"

$oMailman = ObjCreate("Chilkat.MailMan")

; Set the properties for the SMTP server, whatever they may be..
$oMailman.SmtpHost = "smtp.example.com"
$oMailman.SmtpPort = 587
$oMailman.StartTLS = True

$oMailman.SmtpUsername = "myLogin"
$oMailman.OAuth2AccessToken = $sOauth2_access_token

; Create a new email object
$oEmail = ObjCreate("Chilkat.Email")

$oEmail.Subject = "This is a test"
$oEmail.Body = "This is a test"
$oEmail.From = "Joe Example <joe@example.com>"
$oEmail.AddTo("Chilkat Admin","admin@chilkatsoft.com")

; Connect to the server indicated by the SmtpHost property
$bSuccess = $oMailman.SmtpConnect()
If ($bSuccess <> True) Then
    ConsoleWrite($oMailman.LastErrorText & @CRLF)
    Exit
EndIf

; Authenticate using XOAUTH2 (using the OAuth2AccessToken)
$bSuccess = $oMailman.SmtpAuthenticate()
If ($bSuccess <> True) Then
    ConsoleWrite($oMailman.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oMailman.SendEmail($oEmail)
If ($bSuccess <> True) Then
    ConsoleWrite($oMailman.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Email sent." & @CRLF)

$oMailman.CloseSmtpConnection()