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.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
String sMailbox
Handle hoImap
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComLoadFile Of hoToken "gmail-mail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read gmail-mail-token.json from the private working directory."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "An OAuth access token is required. Authorize or refresh the account first."
Procedure_Return
End
Move "user@example.com" To sMailbox
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Set ComRequireSslCertVerify Of hoImap To True
Set ComAuthMethod Of hoImap To "XOAUTH2"
Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Gmail IMAP TLS connection."
Procedure_Return
End
Get ComStringOf Of hoToken "access_token" To sTemp1
Get ComLogin Of hoImap sMailbox sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComDisconnect Of hoImap To iSuccess
Showln "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
Procedure_Return
End
// EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
Get ComExamineMailbox Of hoImap "INBOX" To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not examine INBOX."
Procedure_Return
End
Get ComNumMessages Of hoImap To iTemp1
Showln "Messages in INBOX: " iTemp1
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
If (iSuccess <> True) Begin
Showln "The read-only operation completed, but IMAP logout failed."
End
End_Procedure