Sample code for 30+ languages & platforms
Unicode C

Scan for Emails with Attachments and Save Attachments to Files

Scan for emails with attachments and save attachments.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    BOOL fetchUids;
    HCkMessageSetW messageSet;
    HCkEmailBundleW bundle;
    BOOL headersOnly;
    HCkEmailW fullEmail;
    HCkEmailW emailHeader;
    int i;
    int numAttach;
    const wchar_t *uidStr;
    int uid;
    int j;
    const wchar_t *filename;

    success = FALSE;

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

    // We can choose to fetch UIDs or sequence numbers.
    fetchUids = TRUE;

    // Get the message IDs of all the emails in the mailbox
    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;
    }

    // Fetch the email headers into a bundle object:
    bundle = CkEmailBundleW_Create();
    headersOnly = TRUE;
    success = CkImapW_FetchMsgSet(imap,headersOnly,messageSet,bundle);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messageSet);
        CkEmailBundleW_Dispose(bundle);
        return;
    }

    // Scan for emails with attachments, and save the attachments
    // to a sub-directory.
    fullEmail = CkEmailW_Create();
    emailHeader = CkEmailW_Create();
    i = 0;
    while (i < CkEmailBundleW_getMessageCount(bundle)) {
        // The bundle contains email headers..
        CkEmailBundleW_EmailAt(bundle,i,emailHeader);

        // Does this email have attachments?
        // Use GetMailNumAttach because the attachments
        // are not actually in the email object because
        // we only downloaded headers.
        numAttach = CkImapW_GetMailNumAttach(imap,emailHeader);

        if (numAttach > 0) {
            // Download the entire email and save the
            // attachments. (Remember, we 
            // need to download the entire email because
            // only the headers were previously downloaded.

            // The ckx-imap-uid header field is added when
            // headers are downloaded.  This makes it possible
            // to get the UID from the email object.
            uidStr = CkEmailW_getHeaderField(emailHeader,L"ckx-imap-uid");
            uid = wcstol(uidStr);

            success = CkImapW_FetchEmail(imap,FALSE,uid,TRUE,fullEmail);
            if (success == FALSE) {
                wprintf(L"%s\n",CkImapW_lastErrorText(imap));
                CkImapW_Dispose(imap);
                CkMessageSetW_Dispose(messageSet);
                CkEmailBundleW_Dispose(bundle);
                CkEmailW_Dispose(fullEmail);
                CkEmailW_Dispose(emailHeader);
                return;
            }

            success = CkEmailW_SaveAllAttachments(fullEmail,L"attachmentsDir");

            for (j = 0; j <= numAttach - 1; j++) {
                filename = CkImapW_getMailAttachFilename(imap,emailHeader,j);
                wprintf(L"%s\n",filename);
            }

        }

        i = i + 1;
    }

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


    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(messageSet);
    CkEmailBundleW_Dispose(bundle);
    CkEmailW_Dispose(fullEmail);
    CkEmailW_Dispose(emailHeader);

    }