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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
outFile.WriteLine("Could not read gmail-mail-token.json from the private working directory.")
WScript.Quit
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
outFile.WriteLine("An OAuth access token is required. Authorize or refresh the account first.")
WScript.Quit
End If
mailbox = "user@example.com"
set imap = CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> 1) Then
outFile.WriteLine("Could not establish the Gmail IMAP TLS connection.")
WScript.Quit
End If
success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> 1) Then
success = imap.Disconnect()
outFile.WriteLine("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
WScript.Quit
End If
' EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
success = imap.ExamineMailbox("INBOX")
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
outFile.WriteLine("Could not examine INBOX.")
WScript.Quit
End If
outFile.WriteLine("Messages in INBOX: " & imap.NumMessages)
success = imap.Logout()
success = imap.Disconnect()
If (success <> 1) Then
outFile.WriteLine("The read-only operation completed, but IMAP logout failed.")
End If
outFile.Close