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

C
#include <C_CkJsonObject.h>
#include <C_CkImap.h>
#include <C_CkMailboxes.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject token;
    const char *mailbox;
    HCkImap imap;
    HCkMailboxes folders;
    HCkJsonObject special;
    int i;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    token = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(token,"gmail-mail-token.json");
    if (success != TRUE) {
        printf("Could not load the protocol token from the private working directory.\n");
        CkJsonObject_Dispose(token);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(token,"access_token") != 1) || (CkJsonObject_StringOfEquals(token,"access_token","",TRUE) == TRUE)) {
        printf("Authorize or refresh the mailbox first.\n");
        CkJsonObject_Dispose(token);
        return;
    }

    mailbox = "user@example.com";
    imap = CkImap_Create();
    CkImap_putSsl(imap,TRUE);
    CkImap_putPort(imap,993);
    CkImap_putRequireSslCertVerify(imap,TRUE);
    CkImap_putAuthMethod(imap,"XOAUTH2");
    success = CkImap_Connect(imap,"imap.gmail.com");
    if (success != TRUE) {
        printf("Could not establish the Gmail IMAP TLS connection.\n");
        CkJsonObject_Dispose(token);
        CkImap_Dispose(imap);
        return;
    }

    success = CkImap_Login(imap,mailbox,CkJsonObject_stringOf(token,"access_token"));
    if (success != TRUE) {
        CkImap_Disconnect(imap);
        printf("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.\n");
        CkJsonObject_Dispose(token);
        CkImap_Dispose(imap);
        return;
    }

    folders = CkMailboxes_Create();
    success = CkImap_MbxList(imap,FALSE,"","*",folders);
    if (success != TRUE) {
        CkImap_Logout(imap);
        CkImap_Disconnect(imap);
        printf("Could not list Gmail folders.\n");
        CkJsonObject_Dispose(token);
        CkImap_Dispose(imap);
        CkMailboxes_Dispose(folders);
        return;
    }

    //  LIST special-use attributes work with localized folder names. XLIST is obsolete.
    special = CkJsonObject_Create();
    i = 0;
    while ((i < CkMailboxes_getCount(folders))) {
        if (CkMailboxes_IsSelectable(folders,i) == TRUE) {
            printf("%s  %s\n",CkMailboxes_getName(folders,i),CkMailboxes_getFlags(folders,i));
            if (CkMailboxes_HasFlag(folders,i,"\\All") == TRUE) {
                CkJsonObject_UpdateString(special,"allMail",CkMailboxes_getName(folders,i));
            }

            if (CkMailboxes_HasFlag(folders,i,"\\Sent") == TRUE) {
                CkJsonObject_UpdateString(special,"sent",CkMailboxes_getName(folders,i));
            }

            if (CkMailboxes_HasFlag(folders,i,"\\Drafts") == TRUE) {
                CkJsonObject_UpdateString(special,"drafts",CkMailboxes_getName(folders,i));
            }

            if (CkMailboxes_HasFlag(folders,i,"\\Junk") == TRUE) {
                CkJsonObject_UpdateString(special,"spam",CkMailboxes_getName(folders,i));
            }

            if (CkMailboxes_HasFlag(folders,i,"\\Trash") == TRUE) {
                CkJsonObject_UpdateString(special,"trash",CkMailboxes_getName(folders,i));
            }

            if (CkMailboxes_HasFlag(folders,i,"\\Flagged") == TRUE) {
                CkJsonObject_UpdateString(special,"starred",CkMailboxes_getName(folders,i));
            }

            if (CkMailboxes_HasFlag(folders,i,"\\Important") == TRUE) {
                CkJsonObject_UpdateString(special,"important",CkMailboxes_getName(folders,i));
            }

        }

        i = i + 1;
    }

    CkImap_Logout(imap);
    CkImap_Disconnect(imap);
    CkJsonObject_putEmitCompact(special,FALSE);
    printf("%s\n",CkJsonObject_emit(special));
    //  Missing keys mean the folder was not advertised. Do not substitute an English name.


    CkJsonObject_Dispose(token);
    CkImap_Dispose(imap);
    CkMailboxes_Dispose(folders);
    CkJsonObject_Dispose(special);

    }