Sample code for 30+ languages & platforms
Unicode C

Fetch a Single Message's MIME into a StringBuilder

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a StringBuilder. The first argument is the message id, the second (bUid) selects UID vs sequence number, and the third is the StringBuilder, which is cleared first. This example downloads a message by UID and prints the MIME length.

Background: Chilkat interprets the received MIME as UTF-8. Messages using Base64 or quoted-printable transfer encoding are safe because their encoded bytes are ASCII. Raw 8bit or binary content, or unencoded text in a charset such as ISO-8859-1 or Shift_JIS, can be misinterpreted — in those cases use FetchSingleBd to get the exact bytes.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    BOOL wantUids;
    HCkMessageSetW msgSet;
    unsigned long uid;
    BOOL useUid;
    HCkStringBuilderW sbMime;

    success = FALSE;

    //  Demonstrates the Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a
    //  StringBuilder.  The 1st argument is the message id, the 2nd (bUid) selects UID vs sequence
    //  number, and the 3rd is the StringBuilder (cleared first).

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

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

    //  Search for the message ids to download, returning UIDs.
    wantUids = TRUE;
    msgSet = CkMessageSetW_Create();
    success = CkImapW_QueryMbx(imap,L"ALL",wantUids,msgSet);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(msgSet);
        return;
    }

    if (CkMessageSetW_getCount(msgSet) > 0) {
        uid = CkMessageSetW_GetId(msgSet,0);

        //  Download the message's MIME into a StringBuilder.  The id is a UID.
        useUid = TRUE;
        sbMime = CkStringBuilderW_Create();
        success = CkImapW_FetchSingleAsMimeSb(imap,uid,useUid,sbMime);
        if (success == FALSE) {
            wprintf(L"%s\n",CkImapW_lastErrorText(imap));
            CkImapW_Dispose(imap);
            CkMessageSetW_Dispose(msgSet);
            CkStringBuilderW_Dispose(sbMime);
            return;
        }

        wprintf(L"MIME length: %d chars\n",CkStringBuilderW_getLength(sbMime));
    }

    success = CkImapW_Disconnect(imap);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(msgSet);
        CkStringBuilderW_Dispose(sbMime);
        return;
    }



    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(msgSet);
    CkStringBuilderW_Dispose(sbMime);

    }