Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.1.0+

Gmail IMAP: Search with Gmail Search Syntax

See more GMail SMTP/IMAP/POP Examples

Search with Gmail operators through X-GM-RAW, using QueryMbx to obtain UIDs. The example discovers All Mail and opens it read-only. It quotes the query for the IMAP command rather than URL-encoding it.

IMAP search is limited to the selected mailbox. All Mail normally covers inbox and archived messages, but excludes Spam and Trash; a Gmail search operator cannot expand an IMAP search beyond its selected mailbox. An empty result is successful. Returned IMAP UIDs are mailbox-specific and are not Gmail message IDs. Persist them only with their mailbox and UIDVALIDITY.

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>
#include <C_CkStringBuilderW.h>
#include <C_CkMessageSetW.h>

void ChilkatSample(void)
    {
    BOOL success;
    int replacements;
    HCkJsonObjectW token;
    const wchar_t *mailbox;
    HCkImapW imap;
    const wchar_t *capabilities;
    HCkMailboxesW folders;
    const wchar_t *allMail;
    int folderMatches;
    int f;
    const wchar_t *query;
    HCkStringBuilderW quoted;
    HCkStringBuilderW joined1;
    const wchar_t *criteria;
    HCkMessageSetW found;

    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;
    }

    capabilities = CkImapW_capability(imap);
    if ((CkImapW_getLastMethodSuccess(imap) != TRUE) || (CkImapW_HasCapability(imap,L"X-GM-EXT-1",capabilities) != TRUE)) {
        CkImapW_Logout(imap);
        CkImapW_Disconnect(imap);
        wprintf(L"Gmail IMAP extensions are unavailable.\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;
    }

    allMail = L"";
    folderMatches = 0;
    f = 0;
    while ((f < CkMailboxesW_getCount(folders))) {
        if ((CkMailboxesW_HasFlag(folders,f,L"\\All") == TRUE) && (CkMailboxesW_IsSelectable(folders,f) == TRUE)) {
            allMail = CkMailboxesW_getName(folders,f);
            folderMatches = folderMatches + 1;
        }

        f = f + 1;
    }

    if (folderMatches != 1) {
        CkImapW_Logout(imap);
        CkImapW_Disconnect(imap);
        wprintf(L"Expected one selectable folder with the \\All special-use attribute. Check label visibility.\n");
        CkJsonObjectW_Dispose(token);
        CkImapW_Dispose(imap);
        CkMailboxesW_Dispose(folders);
        return;
    }

    success = CkImapW_ExamineMailbox(imap,allMail);
    if (success != TRUE) {
        CkImapW_Logout(imap);
        CkImapW_Disconnect(imap);
        wprintf(L"Could not open All Mail read-only.\n");
        CkJsonObjectW_Dispose(token);
        CkImapW_Dispose(imap);
        CkMailboxesW_Dispose(folders);
        return;
    }

    //  This search uses Gmail syntax inside a quoted IMAP search argument.
    query = L"has:attachment from:sender@example.net";
    quoted = CkStringBuilderW_Create();
    CkStringBuilderW_Append(quoted,query);
    if ((CkStringBuilderW_Contains(quoted,L"\r",TRUE) == TRUE) || (CkStringBuilderW_Contains(quoted,L"\n",TRUE) == TRUE)) {
        CkImapW_Logout(imap);
        CkImapW_Disconnect(imap);
        wprintf(L"A search query must not contain CR or LF.\n");
        CkJsonObjectW_Dispose(token);
        CkImapW_Dispose(imap);
        CkMailboxesW_Dispose(folders);
        CkStringBuilderW_Dispose(quoted);
        return;
    }

    replacements = CkStringBuilderW_Replace(quoted,L"\\",L"\\\\");
    replacements = CkStringBuilderW_Replace(quoted,L"\"",L"\\\"");
    joined1 = CkStringBuilderW_Create();
    CkStringBuilderW_Append(joined1,L"X-GM-RAW \"");
    CkStringBuilderW_Append(joined1,CkStringBuilderW_getAsString(quoted));
    CkStringBuilderW_Append(joined1,L"\"");
    criteria = CkStringBuilderW_getAsString(joined1);
    found = CkMessageSetW_Create();
    success = CkImapW_QueryMbx(imap,criteria,TRUE,found);
    if (success != TRUE) {
        CkImapW_Logout(imap);
        CkImapW_Disconnect(imap);
        wprintf(L"Gmail search failed.\n");
        CkJsonObjectW_Dispose(token);
        CkImapW_Dispose(imap);
        CkMailboxesW_Dispose(folders);
        CkStringBuilderW_Dispose(quoted);
        CkStringBuilderW_Dispose(joined1);
        CkMessageSetW_Dispose(found);
        return;
    }

    //  Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
    wprintf(L"Matching messages: %d\n",CkMessageSetW_getCount(found));
    wprintf(L"UID set: %s\n",CkMessageSetW_toCompactString(found));
    CkImapW_Logout(imap);
    CkImapW_Disconnect(imap);


    CkJsonObjectW_Dispose(token);
    CkImapW_Dispose(imap);
    CkMailboxesW_Dispose(folders);
    CkStringBuilderW_Dispose(quoted);
    CkStringBuilderW_Dispose(joined1);
    CkMessageSetW_Dispose(found);

    }