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

Visual FoxPro
LOCAL lnSuccess
LOCAL loToken
LOCAL lcMailbox
LOCAL loImap
LOCAL loFolders
LOCAL loSpecial
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 load the protocol token from the private working directory."
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
    ? "Authorize or refresh the mailbox 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, scope, expiry, and account policy."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

loFolders = CreateObject('Chilkat.Mailboxes')
lnSuccess = loImap.MbxList(0,"","*",loFolders)
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Could not list Gmail folders."
    RELEASE loToken
    RELEASE loImap
    RELEASE loFolders
    CANCEL
ENDIF

*  LIST special-use attributes work with localized folder names. XLIST is obsolete.
loSpecial = CreateObject('Chilkat.JsonObject')
i = 0
DO WHILE (i < loFolders.Count)
    IF (loFolders.IsSelectable(i) = 1) THEN
        ? loFolders.GetName(i) + "  " + loFolders.GetFlags(i)
        IF (loFolders.HasFlag(i,"\All") = 1) THEN
            loSpecial.UpdateString("allMail",loFolders.GetName(i))
        ENDIF

        IF (loFolders.HasFlag(i,"\Sent") = 1) THEN
            loSpecial.UpdateString("sent",loFolders.GetName(i))
        ENDIF

        IF (loFolders.HasFlag(i,"\Drafts") = 1) THEN
            loSpecial.UpdateString("drafts",loFolders.GetName(i))
        ENDIF

        IF (loFolders.HasFlag(i,"\Junk") = 1) THEN
            loSpecial.UpdateString("spam",loFolders.GetName(i))
        ENDIF

        IF (loFolders.HasFlag(i,"\Trash") = 1) THEN
            loSpecial.UpdateString("trash",loFolders.GetName(i))
        ENDIF

        IF (loFolders.HasFlag(i,"\Flagged") = 1) THEN
            loSpecial.UpdateString("starred",loFolders.GetName(i))
        ENDIF

        IF (loFolders.HasFlag(i,"\Important") = 1) THEN
            loSpecial.UpdateString("important",loFolders.GetName(i))
        ENDIF

    ENDIF

    i = i + 1
ENDDO
loImap.Logout()
loImap.Disconnect()
loSpecial.EmitCompact = 0
? loSpecial.Emit()
*  Missing keys mean the folder was not advertised. Do not substitute an English name.

RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loSpecial