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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    String sMailbox
    Handle hoImap
    UInteger iSequence
    String sFlags
    Variant vFolders
    Handle hoFolders
    Integer i
    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

    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

    //  Capture only the EXAMINE response, after authentication has completed.
    Get ComLastResponse Of hoImap To sTemp1
    Showln sTemp1
    //  Sample flags without marking messages read. Sequence numbers are session-local.
    Get ComNumMessages Of hoImap To iTemp1
    If (iTemp1 > 0) Begin
        Move 1 To iSequence
        Get ComFetchFlags Of hoImap iSequence False To sFlags
        Get ComLastMethodSuccess Of hoImap To bTemp1
        If (bTemp1 <> True) Begin
            Get ComLogout Of hoImap To iSuccess
            Get ComDisconnect Of hoImap To iSuccess
            Showln "Could not fetch flags; the message may have changed or disappeared."
            Procedure_Return
        End

        Showln "Sequence " iSequence ": " sFlags
    End

    Get Create (RefClass(cComChilkatMailboxes)) To hoFolders
    If (Not(IsComObjectCreated(hoFolders))) Begin
        Send CreateComObject of hoFolders
    End
    Get pvComObject of hoFolders to vFolders
    Get ComMbxList Of hoImap False "" "*" vFolders To iSuccess
    If (iSuccess <> True) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "Could not list folders."
        Procedure_Return
    End

    Move 0 To i
    While (i < (ComCount(hoFolders)))
        Get ComHasFlag Of hoFolders i "\Junk" To bTemp1
        If (bTemp1 = True) Begin
            Get ComGetName Of hoFolders i To sTemp1
            Showln "Spam folder: " sTemp1
        End

        Move (i + 1) To i
    Loop

    Get ComLogout Of hoImap To iSuccess
    Get ComDisconnect Of hoImap To iSuccess


End_Procedure