Sample code for 30+ languages & platforms
Unicode C

IMAP Download All Email One at a Time

Demonstrates how to download every email in an IMAP mailbox one at a time as a MIME string or as an email object. (The MIME contains the full contents of the email including all attachments.)

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    BOOL bUid;
    const wchar_t *mimeStr;
    int i;
    int n;
    HCkEmailW email;

    success = FALSE;

    imap = CkImapW_Create();

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

    // 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"myLogin",L"myPassword");
    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;
    }

    // Once the mailbox is selected, the NumMessages property
    // will contain the number of messages in the mailbox.
    // You may loop from 1 to NumMessages to
    // fetch each message by sequence number.

    bUid = FALSE;

    n = CkImapW_getNumMessages(imap);
    for (i = 1; i <= n; i++) {

        // Download the email by sequence number.
        mimeStr = CkImapW_fetchSingleAsMime(imap,i,bUid);

        // ... your application may process each MIME string...
    }

    // An alternative is to download each email in the form of an
    // email object, like this:
    email = CkEmailW_Create();
    for (i = 1; i <= n; i++) {

        // Download the email by sequence number.
        success = CkImapW_FetchEmail(imap,FALSE,i,bUid,email);

        // ... your application may process the email object...

    }

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


    CkImapW_Dispose(imap);
    CkEmailW_Dispose(email);

    }