Sample code for 30+ languages & platforms
Unicode C

Using Client Certificate w/ IMAP SSL

Demonstrates how to use a client-side certificate with an IMAP SSL connection. The SetSslClientCert method is called to specify a certificate to be used for the SSL connection.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    HCkCertStoreW certStore;
    HCkJsonObjectW jsonCN;
    HCkCertW cert;
    HCkCertW cert2;
    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 SSL connection, set SSL and the port:
    CkImapW_putSsl(imap,TRUE);
    // The typical port for IMAP SSL is 993
    CkImapW_putPort(imap,993);

    // Load a certificate from a PFX file and use it.
    // Note: Other methods are available to load pre-installed
    // certificates from registry-based certificate stores.

    // Create an instance of a certificate store object, load a PFX file,
    // locate the certificate we need, and use it for signing.
    // (a PFX file may contain more than one certificate.)
    certStore = CkCertStoreW_Create();
    // The 1st argument is the filename, the 2nd arg is the 
    // PFX file's password:
    success = CkCertStoreW_LoadPfxFile(certStore,L"myCertWithPrivateKey.pfx",L"secret");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore));
        CkImapW_Dispose(imap);
        CkCertStoreW_Dispose(certStore);
        return;
    }

    // Find the certificate by the subject common name:
    jsonCN = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(jsonCN,L"CN",L"cert common name");

    cert = CkCertW_Create();
    success = CkCertStoreW_FindCert(certStore,jsonCN,cert);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore));
        CkImapW_Dispose(imap);
        CkCertStoreW_Dispose(certStore);
        CkJsonObjectW_Dispose(jsonCN);
        CkCertW_Dispose(cert);
        return;
    }

    // If a PFX file is known to contain a single certificate,
    // you may load it directly into a Chilkat certificate object.
    // This snippet of source code shows how:
    cert2 = CkCertW_Create();
    // The 1st argument is the filename, the 2nd arg is the 
    // PFX file's password:
    success = CkCertW_LoadPfxFile(cert2,L"myClientCert.pfx",L"secret");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkImapW_Dispose(imap);
        CkCertStoreW_Dispose(certStore);
        CkJsonObjectW_Dispose(jsonCN);
        CkCertW_Dispose(cert);
        CkCertW_Dispose(cert2);
        return;
    }

    // Use the cert:
    success = CkImapW_SetSslClientCert(imap,cert);

    // 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);
        CkCertStoreW_Dispose(certStore);
        CkJsonObjectW_Dispose(jsonCN);
        CkCertW_Dispose(cert);
        CkCertW_Dispose(cert2);
        return;
    }

    // Login
    success = CkImapW_Login(imap,L"myLogin",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkCertStoreW_Dispose(certStore);
        CkJsonObjectW_Dispose(jsonCN);
        CkCertW_Dispose(cert);
        CkCertW_Dispose(cert2);
        return;
    }

    // Select an IMAP mailbox
    success = CkImapW_SelectMailbox(imap,L"Inbox");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkCertStoreW_Dispose(certStore);
        CkJsonObjectW_Dispose(jsonCN);
        CkCertW_Dispose(cert);
        CkCertW_Dispose(cert2);
        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);
        CkCertStoreW_Dispose(certStore);
        CkJsonObjectW_Dispose(jsonCN);
        CkCertW_Dispose(cert);
        CkCertW_Dispose(cert2);
        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);
        CkCertStoreW_Dispose(certStore);
        CkJsonObjectW_Dispose(jsonCN);
        CkCertW_Dispose(cert);
        CkCertW_Dispose(cert2);
        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);
    CkCertStoreW_Dispose(certStore);
    CkJsonObjectW_Dispose(jsonCN);
    CkCertW_Dispose(cert);
    CkCertW_Dispose(cert2);
    CkMessageSetW_Dispose(messageSet);
    CkEmailBundleW_Dispose(bundle);
    CkEmailW_Dispose(email);

    }