Sample code for 30+ languages & platforms
C

Scan for Emails with Attachments and Save Attachments to Files

Scan for emails with attachments and save attachments.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImap imap;
    BOOL fetchUids;
    HCkMessageSet messageSet;
    HCkEmailBundle bundle;
    BOOL headersOnly;
    HCkEmail fullEmail;
    HCkEmail emailHeader;
    int i;
    int numAttach;
    const char *uidStr;
    int uid;
    int j;
    const char *filename;

    success = FALSE;

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

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

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

    //  Fetch the email headers into a bundle object:
    bundle = CkEmailBundle_Create();
    headersOnly = TRUE;
    success = CkImap_FetchMsgSet(imap,headersOnly,messageSet,bundle);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkMessageSet_Dispose(messageSet);
        CkEmailBundle_Dispose(bundle);
        return;
    }

    //  Scan for emails with attachments, and save the attachments
    //  to a sub-directory.
    fullEmail = CkEmail_Create();
    emailHeader = CkEmail_Create();
    i = 0;
    while (i < CkEmailBundle_getMessageCount(bundle)) {
        //  The bundle contains email headers..
        CkEmailBundle_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 = CkImap_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 = CkEmail_getHeaderField(emailHeader,"ckx-imap-uid");
            uid = atoi(uidStr);

            success = CkImap_FetchEmail(imap,FALSE,uid,TRUE,fullEmail);
            if (success == FALSE) {
                printf("%s\n",CkImap_lastErrorText(imap));
                CkImap_Dispose(imap);
                CkMessageSet_Dispose(messageSet);
                CkEmailBundle_Dispose(bundle);
                CkEmail_Dispose(fullEmail);
                CkEmail_Dispose(emailHeader);
                return;
            }

            success = CkEmail_SaveAllAttachments(fullEmail,"attachmentsDir");

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

        }

        i = i + 1;
    }

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


    CkImap_Dispose(imap);
    CkMessageSet_Dispose(messageSet);
    CkEmailBundle_Dispose(bundle);
    CkEmail_Dispose(fullEmail);
    CkEmail_Dispose(emailHeader);

    }