Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cOauth2_access_token
LOCAL oMailman
LOCAL oEmail

nSuccess := 0

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

cOauth2_access_token := "some_previously_obtained_token"

oMailman := CreateObject("Chilkat.MailMan")

//  Set the properties for the SMTP server, whatever they may be..
oMailman:SmtpHost := "smtp.example.com"
oMailman:SmtpPort := 587
oMailman:StartTLS := 1

oMailman:SmtpUsername := "myLogin"
oMailman:OAuth2AccessToken := cOauth2_access_token

//  Create a new email object
oEmail := CreateObject("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
nSuccess := oMailman:SmtpConnect()
IF (nSuccess != 1)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  Authenticate using XOAUTH2 (using the OAuth2AccessToken)
nSuccess := oMailman:SmtpAuthenticate()
IF (nSuccess != 1)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oEmail:destroy()
    RETURN
ENDIF

nSuccess := oMailman:SendEmail(oEmail)
IF (nSuccess != 1)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oEmail:destroy()
    RETURN
ENDIF

? "Email sent."

oMailman:CloseSmtpConnection()

oMailman:destroy()
oEmail:destroy()