Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Gmail POP3: Authenticate with OAuth2 and Check Available Mail

See more GMail SMTP/IMAP/POP Examples

Connect to pop.gmail.com:995 over implicit TLS, authenticate with XOAUTH2, and query the number of messages currently available through POP. The example does not retrieve or delete messages. It never prints the POP session log, which can contain an encoded bearer token.

Enable POP in Gmail's Forwarding and POP/IMAP settings and choose whether to make all mail or only new mail available. Workspace policy must also permit POP. Gmail's POP delivery history, delivery window, and batching mean the POP count is not the total number of messages in the account. Review the setting that controls what Gmail does after a message is accessed with POP before adding retrieval code.

For multiple clients, IMAP is usually the better fit. Gmail also provides a POP recent mode for mail from the last 30 days, but this example uses ordinary POP delivery. Gmail's removal of Check mail from other accounts concerns Gmail fetching external providers' mail; it does not remove applications' access to pop.gmail.com. See Google's POP changes FAQ.

This example reads gmail-mail-token.json from a private working directory. Authorize the account first, then refresh the access token before it expires. The separate filename avoids confusing this full-mail grant with a narrower Gmail REST token. Store credentials in protected application storage; never distribute a user's token file with an application.

Gmail SMTP, IMAP, and POP3 use the https://mail.google.com/ OAuth scope. Narrower Gmail REST scopes such as gmail.send or gmail.readonly do not authorize these protocols. Public applications using this restricted scope must meet Google's verification requirements. See Gmail OAuth scopes and XOAUTH2.

Google documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Token
string ls_Mailbox
oleobject loo_Mailman
long li_Count

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Token
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Token.LoadFile("gmail-mail-token.json")
if li_Success <> 1 then
    Write-Debug "Could not read gmail-mail-token.json from the private working directory."
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "An OAuth access token is required. Authorize or refresh the account first."
    destroy loo_Token
    return
end if

ls_Mailbox = "user@example.com"
loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")

loo_Mailman.MailHost = "pop.gmail.com"
loo_Mailman.MailPort = 995
loo_Mailman.PopSsl = 1
loo_Mailman.RequireSslCertVerify = 1
loo_Mailman.PopUsername = ls_Mailbox
loo_Mailman.PopPassword = ""
loo_Mailman.OAuth2AccessToken = loo_Token.StringOf("access_token")
li_Success = loo_Mailman.Pop3Connect()
if li_Success <> 1 then
    Write-Debug "Could not establish the Gmail POP3 TLS connection."
    destroy loo_Token
    destroy loo_Mailman
    return
end if

li_Success = loo_Mailman.Pop3Authenticate()
if li_Success <> 1 then
    loo_Mailman.Pop3EndSessionNoQuit()
    Write-Debug "POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy."
    destroy loo_Token
    destroy loo_Mailman
    return
end if

//  STAT counts currently available POP messages. It does not retrieve or delete them.
li_Count = loo_Mailman.CheckMail()
if li_Count < 0 then
    loo_Mailman.Pop3EndSessionNoQuit()
    Write-Debug "Could not obtain the POP message count."
    destroy loo_Token
    destroy loo_Mailman
    return
end if

Write-Debug "Messages currently available through POP: " + string(li_Count)
//  No DELE commands were issued, so this QUIT commits no deletions.
li_Success = loo_Mailman.Pop3EndSession()
if li_Success <> 1 then
    Write-Debug "The count was obtained, but ending the POP session failed."
end if



destroy loo_Token
destroy loo_Mailman