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

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkImapW.h>
#include <C_CkMailboxesW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW token;
    const wchar_t *mailbox;
    HCkImapW imap;
    HCkMailboxesW folders;
    HCkJsonObjectW special;
    int i;

    success = FALSE;

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

    token = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(token,L"gmail-mail-token.json");
    if (success != TRUE) {
        wprintf(L"Could not load the protocol token from the private working directory.\n");
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"access_token") != 1) || (CkJsonObjectW_StringOfEquals(token,L"access_token",L"",TRUE) == TRUE)) {
        wprintf(L"Authorize or refresh the mailbox first.\n");
        CkJsonObjectW_Dispose(token);
        return;
    }

    mailbox = L"user@example.com";
    imap = CkImapW_Create();
    CkImapW_putSsl(imap,TRUE);
    CkImapW_putPort(imap,993);
    CkImapW_putRequireSslCertVerify(imap,TRUE);
    CkImapW_putAuthMethod(imap,L"XOAUTH2");
    success = CkImapW_Connect(imap,L"imap.gmail.com");
    if (success != TRUE) {
        wprintf(L"Could not establish the Gmail IMAP TLS connection.\n");
        CkJsonObjectW_Dispose(token);
        CkImapW_Dispose(imap);
        return;
    }

    success = CkImapW_Login(imap,mailbox,CkJsonObjectW_stringOf(token,L"access_token"));
    if (success != TRUE) {
        CkImapW_Disconnect(imap);
        wprintf(L"IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.\n");
        CkJsonObjectW_Dispose(token);
        CkImapW_Dispose(imap);
        return;
    }

    folders = CkMailboxesW_Create();
    success = CkImapW_MbxList(imap,FALSE,L"",L"*",folders);
    if (success != TRUE) {
        CkImapW_Logout(imap);
        CkImapW_Disconnect(imap);
        wprintf(L"Could not list Gmail folders.\n");
        CkJsonObjectW_Dispose(token);
        CkImapW_Dispose(imap);
        CkMailboxesW_Dispose(folders);
        return;
    }

    //  LIST special-use attributes work with localized folder names. XLIST is obsolete.
    special = CkJsonObjectW_Create();
    i = 0;
    while ((i < CkMailboxesW_getCount(folders))) {
        if (CkMailboxesW_IsSelectable(folders,i) == TRUE) {
            wprintf(L"%s  %s\n",CkMailboxesW_getName(folders,i),CkMailboxesW_getFlags(folders,i));
            if (CkMailboxesW_HasFlag(folders,i,L"\\All") == TRUE) {
                CkJsonObjectW_UpdateString(special,L"allMail",CkMailboxesW_getName(folders,i));
            }

            if (CkMailboxesW_HasFlag(folders,i,L"\\Sent") == TRUE) {
                CkJsonObjectW_UpdateString(special,L"sent",CkMailboxesW_getName(folders,i));
            }

            if (CkMailboxesW_HasFlag(folders,i,L"\\Drafts") == TRUE) {
                CkJsonObjectW_UpdateString(special,L"drafts",CkMailboxesW_getName(folders,i));
            }

            if (CkMailboxesW_HasFlag(folders,i,L"\\Junk") == TRUE) {
                CkJsonObjectW_UpdateString(special,L"spam",CkMailboxesW_getName(folders,i));
            }

            if (CkMailboxesW_HasFlag(folders,i,L"\\Trash") == TRUE) {
                CkJsonObjectW_UpdateString(special,L"trash",CkMailboxesW_getName(folders,i));
            }

            if (CkMailboxesW_HasFlag(folders,i,L"\\Flagged") == TRUE) {
                CkJsonObjectW_UpdateString(special,L"starred",CkMailboxesW_getName(folders,i));
            }

            if (CkMailboxesW_HasFlag(folders,i,L"\\Important") == TRUE) {
                CkJsonObjectW_UpdateString(special,L"important",CkMailboxesW_getName(folders,i));
            }

        }

        i = i + 1;
    }

    CkImapW_Logout(imap);
    CkImapW_Disconnect(imap);
    CkJsonObjectW_putEmitCompact(special,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(special));
    //  Missing keys mean the folder was not advertised. Do not substitute an English name.


    CkJsonObjectW_Dispose(token);
    CkImapW_Dispose(imap);
    CkMailboxesW_Dispose(folders);
    CkJsonObjectW_Dispose(special);

    }