Sample code for 30+ languages & platforms
Unicode C

Fetch a Chunk of Messages, Tracking Failures

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchChunk2 method, which downloads a run of messages by sequence number and reports which succeeded and which failed. The arguments are the starting sequence number, the count, a MessageSet that receives the failed IDs, a MessageSet that receives the successfully-fetched IDs, and the EmailBundle. This example fetches the first chunk of messages and reports the success and failure counts.

Background: When bulk-downloading a large mailbox, individual messages can occasionally fail to fetch (a corrupt message on the server, a transient error) without failing the whole operation. FetchChunk2 is designed for that reality: it returns a partial EmailBundle plus two MessageSet objects so your code can log or retry exactly the messages that did not come through, rather than aborting the entire sync.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    int numMessages;
    int count;
    HCkMessageSetW failedSet;
    HCkMessageSetW fetchedSet;
    HCkEmailBundleW bundle;

    success = FALSE;

    //  Demonstrates the Imap.FetchChunk2 method, which downloads a run of messages by sequence
    //  number and reports which succeeded and which failed.  The 1st argument is the starting
    //  sequence number, the 2nd is the count, the 3rd receives the failed IDs, the 4th receives
    //  the successfully-fetched IDs, and the 5th is the EmailBundle.

    imap = CkImapW_Create();

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

    success = CkImapW_Login(imap,L"user@example.com",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    numMessages = CkImapW_SelectMailbox(imap,L"Inbox");
    if (numMessages < 0) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  Fetch messages 1..25 (or fewer if the mailbox is smaller).
    count = 25;
    if (count > numMessages) {
        count = numMessages;
    }

    failedSet = CkMessageSetW_Create();
    fetchedSet = CkMessageSetW_Create();
    bundle = CkEmailBundleW_Create();
    success = CkImapW_FetchChunk2(imap,1,count,failedSet,fetchedSet,bundle);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(failedSet);
        CkMessageSetW_Dispose(fetchedSet);
        CkEmailBundleW_Dispose(bundle);
        return;
    }

    wprintf(L"Fetched: %d\n",CkMessageSetW_getCount(fetchedSet));
    wprintf(L"Failed:  %d\n",CkMessageSetW_getCount(failedSet));

    success = CkImapW_Disconnect(imap);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(failedSet);
        CkMessageSetW_Dispose(fetchedSet);
        CkEmailBundleW_Dispose(bundle);
        return;
    }



    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(failedSet);
    CkMessageSetW_Dispose(fetchedSet);
    CkEmailBundleW_Dispose(bundle);

    }