Gmail IMAP: Inspect Flags and Locate the Spam Folder
See more IMAP Examples
Inspect the current INBOX flags and discover Gmail's Spam folder through its \Junk special-use attribute. The example reads the first message's flags, if present, and makes no mailbox changes.
Client-specific Junk/NonJunk keywords are not a portable spam verdict. A mailbox's \Junk LIST attribute identifies a special folder; it is not a message flag. Use current server responses rather than a historical transcript. For Gmail label membership, see X-GM-LABELS.
First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loToken
LOCAL lcMailbox
LOCAL loImap
LOCAL lnSequence
LOCAL lcFlags
LOCAL loFolders
LOCAL i
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-mail-token.json")
IF (lnSuccess <> 1) THEN
? "Could not read gmail-mail-token.json from the private working directory."
RELEASE loToken
CANCEL
ENDIF
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
? "An OAuth access token is required. Authorize or refresh the account first."
RELEASE loToken
CANCEL
ENDIF
lcMailbox = "user@example.com"
loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
loImap.RequireSslCertVerify = 1
loImap.AuthMethod = "XOAUTH2"
lnSuccess = loImap.Connect("imap.gmail.com")
IF (lnSuccess <> 1) THEN
? "Could not establish the Gmail IMAP TLS connection."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
lnSuccess = loImap.Login(lcMailbox,loToken.StringOf("access_token"))
IF (lnSuccess <> 1) THEN
loImap.Disconnect()
? "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
lnSuccess = loImap.ExamineMailbox("INBOX")
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not examine INBOX."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
* Capture only the EXAMINE response, after authentication has completed.
? loImap.LastResponse
* Sample flags without marking messages read. Sequence numbers are session-local.
IF (loImap.NumMessages > 0) THEN
lnSequence = 1
lcFlags = loImap.FetchFlags(lnSequence,0)
IF (loImap.LastMethodSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not fetch flags; the message may have changed or disappeared."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
? "Sequence " + STR(lnSequence) + ": " + lcFlags
ENDIF
loFolders = CreateObject('Chilkat.Mailboxes')
lnSuccess = loImap.MbxList(0,"","*",loFolders)
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not list folders."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
CANCEL
ENDIF
i = 0
DO WHILE (i < loFolders.Count)
IF (loFolders.HasFlag(i,"\Junk") = 1) THEN
? "Spam folder: " + loFolders.GetName(i)
ENDIF
i = i + 1
ENDDO
loImap.Logout()
loImap.Disconnect()
RELEASE loToken
RELEASE loImap
RELEASE loFolders