Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.1.0+

Gmail IMAP: Discover Special Folders

See more GMail SMTP/IMAP/POP Examples

Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.

Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.

Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.

Google documentation

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    String sMailbox
    Handle hoImap
    Variant vFolders
    Handle hoFolders
    Handle hoSpecial
    Integer i
    String sTemp1
    String sTemp2
    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 load the protocol token 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 "Authorize or refresh the mailbox 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, scope, expiry, and account policy."
        Procedure_Return
    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 Gmail folders."
        Procedure_Return
    End

    //  LIST special-use attributes work with localized folder names. XLIST is obsolete.
    Get Create (RefClass(cComChilkatJsonObject)) To hoSpecial
    If (Not(IsComObjectCreated(hoSpecial))) Begin
        Send CreateComObject of hoSpecial
    End
    Move 0 To i
    While (i < (ComCount(hoFolders)))
        Get ComIsSelectable Of hoFolders i To bTemp1
        If (bTemp1 = True) Begin
            Get ComGetName Of hoFolders i To sTemp1
            Get ComGetFlags Of hoFolders i To sTemp2
            Showln sTemp1 "  " sTemp2
            Get ComHasFlag Of hoFolders i "\All" To bTemp1
            If (bTemp1 = True) Begin
                Get ComGetName Of hoFolders i To sTemp1
                Get ComUpdateString Of hoSpecial "allMail" sTemp1 To iSuccess
            End

            Get ComHasFlag Of hoFolders i "\Sent" To bTemp1
            If (bTemp1 = True) Begin
                Get ComGetName Of hoFolders i To sTemp1
                Get ComUpdateString Of hoSpecial "sent" sTemp1 To iSuccess
            End

            Get ComHasFlag Of hoFolders i "\Drafts" To bTemp1
            If (bTemp1 = True) Begin
                Get ComGetName Of hoFolders i To sTemp1
                Get ComUpdateString Of hoSpecial "drafts" sTemp1 To iSuccess
            End

            Get ComHasFlag Of hoFolders i "\Junk" To bTemp1
            If (bTemp1 = True) Begin
                Get ComGetName Of hoFolders i To sTemp1
                Get ComUpdateString Of hoSpecial "spam" sTemp1 To iSuccess
            End

            Get ComHasFlag Of hoFolders i "\Trash" To bTemp1
            If (bTemp1 = True) Begin
                Get ComGetName Of hoFolders i To sTemp1
                Get ComUpdateString Of hoSpecial "trash" sTemp1 To iSuccess
            End

            Get ComHasFlag Of hoFolders i "\Flagged" To bTemp1
            If (bTemp1 = True) Begin
                Get ComGetName Of hoFolders i To sTemp1
                Get ComUpdateString Of hoSpecial "starred" sTemp1 To iSuccess
            End

            Get ComHasFlag Of hoFolders i "\Important" To bTemp1
            If (bTemp1 = True) Begin
                Get ComGetName Of hoFolders i To sTemp1
                Get ComUpdateString Of hoSpecial "important" sTemp1 To iSuccess
            End

        End

        Move (i + 1) To i
    Loop

    Get ComLogout Of hoImap To iSuccess
    Get ComDisconnect Of hoImap To iSuccess
    Set ComEmitCompact Of hoSpecial To False
    Get ComEmit Of hoSpecial To sTemp1
    Showln sTemp1
    //  Missing keys mean the folder was not advertised. Do not substitute an English name.


End_Procedure