Sample code for 30+ languages & platforms
Unicode C

Determine the Number of Unseen Email Messages

Demonstrates how to determine how many unseen messages exist in an email account on an IMAP server.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkImapW.h>
#include <C_CkMessageSetW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    int totalNum;
    BOOL fetchUids;
    HCkMessageSetW messageSet;
    unsigned long numUnseen;
    int i;

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    imap = CkImapW_Create();

    //  Connect to an IMAP server.
    //  Use TLS
    CkImapW_putSsl(imap,TRUE);
    CkImapW_putPort(imap,993);
    success = CkImapW_Connect(imap,L"imap.example.com");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  Login
    success = CkImapW_Login(imap,L"***",L"***");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  Select an IMAP mailbox
    success = CkImapW_SelectMailbox(imap,L"Inbox");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  After selecting the mailbox. the total number of emails
    //  is immediately available:
    totalNum = CkImapW_getNumMessages(imap);
    wprintf(L"Num messages = %d\n",totalNum);

    //  To determine the number of unseen messages, a call
    //  to Search is required, which returns the set of UIDs
    //  of all unseen messages.

    //  We can choose to fetch UIDs or sequence numbers.
    fetchUids = TRUE;
    messageSet = CkMessageSetW_Create();
    success = CkImapW_QueryMbx(imap,L"UNSEEN",fetchUids,messageSet);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messageSet);
        return;
    }

    numUnseen = CkMessageSetW_getCount(messageSet);
    wprintf(L"%u\n",numUnseen);

    wprintf(L"UIDs ----\n");

    //  Display the UIDs
    i = 0;
    while (i < CkMessageSetW_getCount(messageSet)) {
        wprintf(L"%u\n",CkMessageSetW_GetId(messageSet,i));
        i = i + 1;
    }

    //  Disconnect from the IMAP server.
    success = CkImapW_Disconnect(imap);


    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(messageSet);

    }