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

Gmail IMAP: Authenticate and Open the Inbox Read-Only

See more GMail SMTP/IMAP/POP Examples

Connect to imap.gmail.com:993 over implicit TLS, authenticate with XOAUTH2, and examine INBOX without changing messages. ExamineMailbox opens the mailbox read-only; the count is for the selected mailbox, not the entire Gmail account. Logout and disconnect explicitly when finished.

For personal Gmail accounts, IMAP has been always enabled since January 2025; the old Enable IMAP switch is no longer needed. Workspace administrators can restrict IMAP access and allowed OAuth clients. Use the mailbox that authorized the token, not the application's client ID.

Gmail exposes labels as IMAP folders. One message can appear under several labels, so summing folder counts can double-count it. Gmail special-folder names can be localized; INBOX is the protocol-defined exception. Discover other folders through LIST special-use attributes instead of assuming English names. Long-lived connections may be closed when the access token expires; refresh and reconnect.

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_Imap

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_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")

loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.AuthMethod = "XOAUTH2"
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
    Write-Debug "Could not establish the Gmail IMAP TLS connection."
    destroy loo_Token
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login(ls_Mailbox,loo_Token.StringOf("access_token"))
if li_Success <> 1 then
    loo_Imap.Disconnect()
    Write-Debug "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
    destroy loo_Token
    destroy loo_Imap
    return
end if

//  EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
li_Success = loo_Imap.ExamineMailbox("INBOX")
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not examine INBOX."
    destroy loo_Token
    destroy loo_Imap
    return
end if

Write-Debug "Messages in INBOX: " + string(loo_Imap.NumMessages)
li_Success = loo_Imap.Logout()
loo_Imap.Disconnect()
if li_Success <> 1 then
    Write-Debug "The read-only operation completed, but IMAP logout failed."
end if



destroy loo_Token
destroy loo_Imap