Sample code for 30+ languages & platforms
Unicode C

Secure IMAP over TLS/SSL

To communicate with an IMAP server over TLS/SSL, simply set the Ssl and Port properties. The remainder of your code is the same as with non-SSL.

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 email;
    int i;
    int numEmails;

    success = FALSE;

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

    imap = CkImapW_Create();

    // To use a secure TLS/SSL connection, set the Ssl property and the port:
    CkImapW_putSsl(imap,TRUE);
    // The typical port for IMAP SSL is 993
    CkImapW_putPort(imap,993);

    // Connect to an IMAP server.
    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;
    }

    // Get the message IDs of all the emails in the mailbox
    fetchUids = TRUE;
    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 emails into a bundle object:
    bundle = CkEmailBundleW_Create();
    headersOnly = FALSE;
    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;
    }

    // Loop over the bundle and display the FROM and SUBJECT of each.
    email = CkEmailW_Create();
    i = 0;
    numEmails = CkEmailBundleW_getMessageCount(bundle);
    while (i < numEmails) {
        CkEmailBundleW_EmailAt(bundle,i,email);

        wprintf(L"%s\n",CkEmailW_ck_from(email));
        wprintf(L"%s\n",CkEmailW_subject(email));
        wprintf(L"--\n");
        i = i + 1;
    }

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


    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(messageSet);
    CkEmailBundleW_Dispose(bundle);
    CkEmailW_Dispose(email);

    }