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

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.

Reference documentation

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMailboxes.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    token.i = CkJsonObject::ckCreate()
    If token.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(token,"gmail-mail-token.json")
    If success <> 1
        Debug "Could not read gmail-mail-token.json from the private working directory."
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"access_token","",1) = 1)
        Debug "An OAuth access token is required. Authorize or refresh the account first."
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    mailbox.s = "user@example.com"
    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    CkImap::setCkRequireSslCertVerify(imap, 1)
    CkImap::setCkAuthMethod(imap, "XOAUTH2")
    success = CkImap::ckConnect(imap,"imap.gmail.com")
    If success <> 1
        Debug "Could not establish the Gmail IMAP TLS connection."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,mailbox,CkJsonObject::ckStringOf(token,"access_token"))
    If success <> 1
        CkImap::ckDisconnect(imap)
        Debug "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckExamineMailbox(imap,"INBOX")
    If success <> 1
        CkImap::ckLogout(imap)
        CkImap::ckDisconnect(imap)
        Debug "Could not examine INBOX."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Capture only the EXAMINE response, after authentication has completed.
    Debug CkImap::ckLastResponse(imap)
    ;  Sample flags without marking messages read. Sequence numbers are session-local.
    If CkImap::ckNumMessages(imap) > 0
        sequence.i = 1
        flags.s = CkImap::ckFetchFlags(imap,sequence,0)
        If CkImap::ckLastMethodSuccess(imap) <> 1
            CkImap::ckLogout(imap)
            CkImap::ckDisconnect(imap)
            Debug "Could not fetch flags; the message may have changed or disappeared."
            CkJsonObject::ckDispose(token)
            CkImap::ckDispose(imap)
            ProcedureReturn
        EndIf

        Debug "Sequence " + Str(sequence) + ": " + flags
    EndIf

    folders.i = CkMailboxes::ckCreate()
    If folders.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckMbxList(imap,0,"","*",folders)
    If success <> 1
        CkImap::ckLogout(imap)
        CkImap::ckDisconnect(imap)
        Debug "Could not list folders."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        CkMailboxes::ckDispose(folders)
        ProcedureReturn
    EndIf

    i.i = 0
    While (i < CkMailboxes::ckCount(folders))
        If CkMailboxes::ckHasFlag(folders,i,"\Junk") = 1
            Debug "Spam folder: " + CkMailboxes::ckGetName(folders,i)
        EndIf

        i = i + 1
    Wend
    CkImap::ckLogout(imap)
    CkImap::ckDisconnect(imap)


    CkJsonObject::ckDispose(token)
    CkImap::ckDispose(imap)
    CkMailboxes::ckDispose(folders)


    ProcedureReturn
EndProcedure