Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token [new_CkJsonObject]

set success [CkJsonObject_LoadFile $token "gmail-mail-token.json"]
if {$success != 1} then {
    puts "Could not read gmail-mail-token.json from the private working directory."
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
    puts "An OAuth access token is required. Authorize or refresh the account first."
    delete_CkJsonObject $token
    exit
}

set mailbox "user@example.com"
set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
CkImap_put_RequireSslCertVerify $imap 1
CkImap_put_AuthMethod $imap "XOAUTH2"
set success [CkImap_Connect $imap "imap.gmail.com"]
if {$success != 1} then {
    puts "Could not establish the Gmail IMAP TLS connection."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap $mailbox [CkJsonObject_stringOf $token "access_token"]]
if {$success != 1} then {
    CkImap_Disconnect $imap
    puts "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

#  EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
set success [CkImap_ExamineMailbox $imap "INBOX"]
if {$success != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Could not examine INBOX."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

puts "Messages in INBOX: [CkImap_get_NumMessages $imap]"
set success [CkImap_Logout $imap]
CkImap_Disconnect $imap
if {$success != 1} then {
    puts "The read-only operation completed, but IMAP logout failed."
}


delete_CkJsonObject $token
delete_CkImap $imap