Sample code for 30+ languages & platforms
C

Process New Email by Scanning for Senders

Scan email and save application-selected emails to EML files with unique filenames.

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 emailHeader;
    HCkEmail fullEmail;
    int i;
    int numEmails;
    const char *uidStr;
    int uid;
    const char *filename;

    success = FALSE;

    //  This example assumes 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;

    //  Fetch messages from the mailbox using a search criteria.
    //  This example finds NEW emails: these are emails that have the RECENT flag set, but not the SEEN flag:
    messageSet = CkMessageSet_Create();
    success = CkImap_QueryMbx(imap,"NEW",fetchUids,messageSet);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkMessageSet_Dispose(messageSet);
        return;
    }

    //  This example will download headers, and then download
    //  the full email for those emails sent from a contact
    //  in our database.

    //  When downloading headers, each email object contains
    //  (obviously) the headers, but the body will be missing.
    //  Also, attachments will not be included.  However, it is
    //  possible to get information about the attachments
    //  as well as the complete size of the email.
    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;
    }

    //  Loop over the email objects...
    emailHeader = CkEmail_Create();
    fullEmail = CkEmail_Create();
    i = 0;
    numEmails = CkEmailBundle_getMessageCount(bundle);
    while (i < numEmails) {
        CkEmailBundle_EmailAt(bundle,i,emailHeader);

        //  The sender's email address and name are available
        //  in the From, FromAddress, and FromName properties.
        //  If the sender is "Chilkat Support <support@chilkatsoft.com",
        //  then the From property will hold the entire string.
        //  the FromName property contains"Chilkat Support",
        //  and the FromAddress property contains "support@chilkatsoft.com";
        printf("%s\n",CkEmail_ck_from(emailHeader));
        printf("%s\n",CkEmail_fromAddress(emailHeader));
        printf("%s\n",CkEmail_fromName(emailHeader));

        //  Assume at this point your code checks to see if the sender
        //  is one in your contacts database.  If so, this is
        //  the code you would write to download the entire
        //  email and save it to a file.

        //  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(emailHeader);
            CkEmail_Dispose(fullEmail);
            return;
        }

        //  You can use the GenerateFilename method to
        //  generate a unique filename...
        filename = CkEmail_generateFilename(fullEmail);

        //  SaveEml saves the entire email, including attachments.
        success = CkEmail_SaveEml(fullEmail,filename);

        printf("--\n");

        i = i + 1;
    }

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


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

    }