Sample code for 30+ languages & platforms
Xbase++

POP3 GMail XOAUTH2 Authentication

See more GMail SMTP/IMAP/POP Examples

Demonstrates using OAuth2 authentication with pop.gmail.com.

Note: This example requires Chilkat v9.5.0.83 or greater.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMailman
LOCAL oJsonToken
LOCAL nNumEmails

nSuccess := 0

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

oMailman := CreateObject("Chilkat.MailMan")

oMailman:MailHost := "pop.gmail.com"
oMailman:MailPort := 995
oMailman:PopSsl := 1

oMailman:PopUsername := "my_account@gmail.com"

//  If using OAuth2 authentication, leave the password empty.
oMailman:PopPassword := ""

//  Load our previously obtained OAuth2 access token.
//  See Getting a GMail POP3 OAuth2 Access Token
oJsonToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonToken:LoadFile("qa_data/tokens/gmail_pop3.json")
IF (nSuccess == 0)
    ? oJsonToken:LastErrorText
    oMailman:destroy()
    oJsonToken:destroy()
    RETURN
ENDIF

oMailman:OAuth2AccessToken := oJsonToken:StringOf("access_token")

//  Make the TLS connection to the POP3 server.
nSuccess := oMailman:Pop3Connect()
IF (nSuccess != 1)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oJsonToken:destroy()
    RETURN
ENDIF

//  Authenticate..
nSuccess := oMailman:Pop3Authenticate()
IF (nSuccess != 1)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oJsonToken:destroy()
    RETURN
ENDIF

//  Find out how many emails are on the server..
nNumEmails := oMailman:CheckMail()
IF (nNumEmails < 0)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oJsonToken:destroy()
    RETURN
ENDIF

//  Examine the POP3 session log:
? oMailman:Pop3SessionLog

//  The POP3 session log will look something like this:

//  **** Connected to pop.gmail.com:995
//  < +OK Gpop ready for requests from 87.9.200.42 l16mb86351205iok
//  > AUTH XOAUTH2 dXNlcj1....VaMDJFAQE=
//  < +OK Welcome.
//  > STAT
//  < +OK 301 1627357
//  
//  -- Finished.

//  End the POP3 session and close the connection to the GMail server.
nSuccess := oMailman:Pop3EndSession()
IF (nSuccess != 1)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oJsonToken:destroy()
    RETURN
ENDIF

? "-- Finished."

oMailman:destroy()
oJsonToken:destroy()