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 <CkJsonObjectW.h>
#include <CkImapW.h>
#include <CkMailboxesW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkJsonObjectW token;
    success = token.LoadFile(L"gmail-mail-token.json");
    if (success != true) {
        wprintf(L"Could not load the protocol token from the private working directory.\n");
        return;
    }

    if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
        wprintf(L"Authorize or refresh the mailbox first.\n");
        return;
    }

    const wchar_t *mailbox = L"user@example.com";
    CkImapW imap;
    imap.put_Ssl(true);
    imap.put_Port(993);
    imap.put_RequireSslCertVerify(true);
    imap.put_AuthMethod(L"XOAUTH2");
    success = imap.Connect(L"imap.gmail.com");
    if (success != true) {
        wprintf(L"Could not establish the Gmail IMAP TLS connection.\n");
        return;
    }

    success = imap.Login(mailbox,token.stringOf(L"access_token"));
    if (success != true) {
        imap.Disconnect();
        wprintf(L"IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.\n");
        return;
    }

    CkMailboxesW folders;
    success = imap.MbxList(false,L"",L"*",folders);
    if (success != true) {
        imap.Logout();
        imap.Disconnect();
        wprintf(L"Could not list Gmail folders.\n");
        return;
    }

    //  LIST special-use attributes work with localized folder names. XLIST is obsolete.
    CkJsonObjectW special;
    int i = 0;
    while ((i < folders.get_Count())) {
        if (folders.IsSelectable(i) == true) {
            wprintf(L"%s  %s\n",folders.getName(i),folders.getFlags(i));
            if (folders.HasFlag(i,L"\\All") == true) {
                special.UpdateString(L"allMail",folders.getName(i));
            }

            if (folders.HasFlag(i,L"\\Sent") == true) {
                special.UpdateString(L"sent",folders.getName(i));
            }

            if (folders.HasFlag(i,L"\\Drafts") == true) {
                special.UpdateString(L"drafts",folders.getName(i));
            }

            if (folders.HasFlag(i,L"\\Junk") == true) {
                special.UpdateString(L"spam",folders.getName(i));
            }

            if (folders.HasFlag(i,L"\\Trash") == true) {
                special.UpdateString(L"trash",folders.getName(i));
            }

            if (folders.HasFlag(i,L"\\Flagged") == true) {
                special.UpdateString(L"starred",folders.getName(i));
            }

            if (folders.HasFlag(i,L"\\Important") == true) {
                special.UpdateString(L"important",folders.getName(i));
            }

        }

        i = i + 1;
    }

    imap.Logout();
    imap.Disconnect();
    special.put_EmitCompact(false);
    wprintf(L"%s\n",special.emit());
    //  Missing keys mean the folder was not advertised. Do not substitute an English name.
    }