Sample code for 30+ languages & platforms
PureBasic 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 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 load the protocol token 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 "Authorize or refresh the mailbox 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, scope, expiry, and account policy."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        ProcedureReturn
    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 Gmail folders."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        CkMailboxes::ckDispose(folders)
        ProcedureReturn
    EndIf

    ;  LIST special-use attributes work with localized folder names. XLIST is obsolete.
    special.i = CkJsonObject::ckCreate()
    If special.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    While (i < CkMailboxes::ckCount(folders))
        If CkMailboxes::ckIsSelectable(folders,i) = 1
            Debug CkMailboxes::ckGetName(folders,i) + "  " + CkMailboxes::ckGetFlags(folders,i)
            If CkMailboxes::ckHasFlag(folders,i,"\All") = 1
                CkJsonObject::ckUpdateString(special,"allMail",CkMailboxes::ckGetName(folders,i))
            EndIf

            If CkMailboxes::ckHasFlag(folders,i,"\Sent") = 1
                CkJsonObject::ckUpdateString(special,"sent",CkMailboxes::ckGetName(folders,i))
            EndIf

            If CkMailboxes::ckHasFlag(folders,i,"\Drafts") = 1
                CkJsonObject::ckUpdateString(special,"drafts",CkMailboxes::ckGetName(folders,i))
            EndIf

            If CkMailboxes::ckHasFlag(folders,i,"\Junk") = 1
                CkJsonObject::ckUpdateString(special,"spam",CkMailboxes::ckGetName(folders,i))
            EndIf

            If CkMailboxes::ckHasFlag(folders,i,"\Trash") = 1
                CkJsonObject::ckUpdateString(special,"trash",CkMailboxes::ckGetName(folders,i))
            EndIf

            If CkMailboxes::ckHasFlag(folders,i,"\Flagged") = 1
                CkJsonObject::ckUpdateString(special,"starred",CkMailboxes::ckGetName(folders,i))
            EndIf

            If CkMailboxes::ckHasFlag(folders,i,"\Important") = 1
                CkJsonObject::ckUpdateString(special,"important",CkMailboxes::ckGetName(folders,i))
            EndIf

        EndIf

        i = i + 1
    Wend
    CkImap::ckLogout(imap)
    CkImap::ckDisconnect(imap)
    CkJsonObject::setCkEmitCompact(special, 0)
    Debug CkJsonObject::ckEmit(special)
    ;  Missing keys mean the folder was not advertised. Do not substitute an English name.


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


    ProcedureReturn
EndProcedure