Sample code for 30+ languages & platforms
Unicode C

Fetch 1st N Headers of Search Results

Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    BOOL fetchUids;
    HCkMessageSetW messageSet;
    int numFound;
    int upperBound;
    int i;
    BOOL bUid;
    BOOL headerOnly;
    HCkEmailW email;

    success = FALSE;

    // This example assumes 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;
    }

    // Get the message IDs of all the emails in the mailbox
    // We can choose to fetch UIDs or sequence numbers.
    fetchUids = TRUE;
    messageSet = CkMessageSetW_Create();
    success = CkImapW_QueryMbx(imap,L"ALL",fetchUids,messageSet);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messageSet);
        return;
    }

    numFound = CkMessageSetW_getCount(messageSet);
    if (numFound == 0) {
        wprintf(L"No messages found.\n");
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messageSet);
        return;
    }

    // Get the 1st 10 messages in messageSet.
    upperBound = 10;
    if (numFound < upperBound) {
        upperBound = numFound;
    }

    i = 0;
    bUid = CkMessageSetW_getHasUids(messageSet);
    headerOnly = TRUE;
    email = CkEmailW_Create();

    while (i < upperBound) {

        success = CkImapW_FetchEmail(imap,headerOnly,CkMessageSetW_GetId(messageSet,i),bUid,email);
        if (success == FALSE) {
            wprintf(L"%s\n",CkImapW_lastErrorText(imap));
            CkImapW_Dispose(imap);
            CkMessageSetW_Dispose(messageSet);
            CkEmailW_Dispose(email);
            return;
        }

        wprintf(L"%d: %s\n",i,CkEmailW_subject(email));

        i = i + 1;
    }

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


    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(messageSet);
    CkEmailW_Dispose(email);

    }