Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkImap.h>
#include <C_CkMessageSet.h>
#include <C_CkEmail.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImap imap;
    BOOL fetchUids;
    HCkMessageSet messageSet;
    int numFound;
    int upperBound;
    int i;
    BOOL bUid;
    BOOL headerOnly;
    HCkEmail email;

    success = FALSE;

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

    imap = CkImap_Create();

    // Connect to an IMAP server.
    // Use TLS
    CkImap_putSsl(imap,TRUE);
    CkImap_putPort(imap,993);
    success = CkImap_Connect(imap,"imap.example.com");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    // Login
    success = CkImap_Login(imap,"****","****");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    // Select an IMAP mailbox
    success = CkImap_SelectMailbox(imap,"Inbox");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_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 = CkMessageSet_Create();
    success = CkImap_QueryMbx(imap,"ALL",fetchUids,messageSet);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkMessageSet_Dispose(messageSet);
        return;
    }

    numFound = CkMessageSet_getCount(messageSet);
    if (numFound == 0) {
        printf("No messages found.\n");
        CkImap_Dispose(imap);
        CkMessageSet_Dispose(messageSet);
        return;
    }

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

    i = 0;
    bUid = CkMessageSet_getHasUids(messageSet);
    headerOnly = TRUE;
    email = CkEmail_Create();

    while (i < upperBound) {

        success = CkImap_FetchEmail(imap,headerOnly,CkMessageSet_GetId(messageSet,i),bUid,email);
        if (success == FALSE) {
            printf("%s\n",CkImap_lastErrorText(imap));
            CkImap_Dispose(imap);
            CkMessageSet_Dispose(messageSet);
            CkEmail_Dispose(email);
            return;
        }

        printf("%d: %s\n",i,CkEmail_subject(email));

        i = i + 1;
    }

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


    CkImap_Dispose(imap);
    CkMessageSet_Dispose(messageSet);
    CkEmail_Dispose(email);

    }