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

Set or Clear a Flag on a MessageSet

See more IMAP Examples

Demonstrates the Chilkat Imap.SetFlags method, which sets or clears a flag on every message in a MessageSet. The first argument is the MessageSet, the second is the flag name, and the third is the value (1 to set the flag, 0 to clear it). This example marks all unseen messages as Seen.

Background: IMAP flags are per-message keywords the server stores and shares across all clients. The standard system flags are Seen, Answered, Flagged, Deleted, Draft, and Recent. Give the flag name without the leading backslash here (use "Seen", not "\Seen"). SetFlags applies the change to the whole set in one round trip, which is how a client implements "mark all as read."

Chilkat Unicode C++ Downloads

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

void ChilkatSample(void)
    {
    bool success = false;

    //  Demonstrates the Imap.SetFlags method, which sets or clears a flag on every message in a
    //  MessageSet.  The 1st argument is the MessageSet, the 2nd is the flag name, and the 3rd is
    //  the value (1 to set the flag, 0 to clear it).

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

    //  Find the messages to flag (here, all unseen messages, by UID).
    CkMessageSetW msgSet;
    success = imap.QueryMbx(L"UNSEEN",true,msgSet);
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    //  Mark all of them as seen.  A value of 1 sets the flag; 0 would clear it.
    success = imap.SetFlags(msgSet,L"Seen",1);
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    wprintf(L"Marked %d messages as Seen.\n",msgSet.get_Count());

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