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

Fetch the Flags Set on an IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchFlags method, which returns the flags currently set on a single message as a space-separated string. The first argument is the message ID and the second (bUid) selects UID vs sequence number. This example fetches and prints the flags for one message identified by UID.

Background: The returned string is the server's current flag list for the message, such as \Seen \Flagged. This is a quick way to check a single message's state — for example, to decide whether it still needs processing — without downloading the message itself. To read the same information from a message you already have as an Email object, use GetMailFlag instead.

Chilkat Unicode C++ Downloads

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

void ChilkatSample(void)
    {
    bool success = false;

    //  Demonstrates the Imap.FetchFlags method, which returns the flags currently set on a single
    //  message as a space-separated string.  The 1st argument is the message ID and the 2nd (bUid)
    //  selects UID vs sequence number.

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

    CkMessageSetW msgSet;
    success = imap.QueryMbx(L"ALL",true,msgSet);
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    if (msgSet.get_Count() > 0) {
        unsigned long uid = msgSet.GetId(0);
        //  FetchFlags returns a string, so assign it to a string variable and check for success.
        const wchar_t *flags = imap.fetchFlags(uid,true);
        if (imap.get_LastMethodSuccess() == false) {
            wprintf(L"%s\n",imap.lastErrorText());
            return;
        }

        wprintf(L"UID %u flags: %s\n",uid,flags);
    }

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