Sample code for 30+ languages & platforms
Unicode C++

Store One or More Flags on a Single Message

See more IMAP Examples

Demonstrates the Chilkat Imap.StoreFlags method, which sets or clears one or more flags on a single message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is a space-separated list of flag names, and the fourth is the value (1 to set, 0 to clear). This example applies the Seen and Flagged flags to one message by UID.

Background: StoreFlags is the single-message counterpart to SetFlags and lets you change several flags at once by listing them space-separated (without leading backslashes). Because the ID can be either a UID or a sequence number, the bUid argument must match how you obtained it — a UID from a UID search, or a sequence number from a sequence-based operation. UIDs are the safer choice since they remain valid as the mailbox changes.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkImapW.h>
#include <CkMessageSetW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Demonstrates the Imap.StoreFlags method, which sets or clears one or more flags on a single
    //  message identified by ID.  The 1st argument is the message ID, the 2nd (bUid) selects UID
    //  vs sequence number, the 3rd is a space-separated list of flag names, and the 4th is the
    //  value (1 to set, 0 to clear).

    CkImapW imap;

    imap.put_Ssl(true);
    imap.put_Port(993);

    success = imap.Connect(L"imap.example.com");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    success = imap.Login(L"user@example.com",L"myPassword");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    success = imap.SelectMailbox(L"Inbox");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    //  Get the UIDs of all messages.
    CkMessageSetW msgSet;
    success = imap.QueryMbx(L"ALL",true,msgSet);
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    if (msgSet.get_Count() > 0) {
        //  Apply the Seen and Flagged flags to the first message.  bUid is true because the ID
        //  came from a UID search.
        unsigned long uid = msgSet.GetId(0);
        success = imap.StoreFlags(uid,true,L"Seen Flagged",1);
        if (success == false) {
            wprintf(L"%s\n",imap.lastErrorText());
            return;
        }

        wprintf(L"Updated flags on UID %u\n",uid);
    }

    success = imap.Disconnect();
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }
    }