Sample code for 30+ languages & platforms
Unicode C

Use an XML Certificate Vault with IMAP

See more IMAP Examples

Demonstrates the Chilkat Imap.UseCertVault method, which associates an XmlCertVault with the Imap object as a source of certificates and private keys for S/MIME operations. The only argument is the vault. Only one vault is associated at a time.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: An XmlCertVault is a portable container that can hold multiple certificates and keys in a single (optionally password-protected) XML file. It is a convenient way to ship the exact set of credentials an application needs without depending on the operating system's certificate stores. Associating a vault makes all of its contents available for S/MIME decryption and signature verification.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkImapW.h>
#include <C_CkXmlCertVaultW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    const wchar_t *pfxPassword;
    HCkXmlCertVaultW vault;

    success = FALSE;

    //  Demonstrates the Imap.UseCertVault method, which associates an XmlCertVault with the Imap
    //  object as a source of certificates and private keys for S/MIME operations.  The only
    //  argument is the XmlCertVault.

    imap = CkImapW_Create();

    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;
    }

    success = CkImapW_Login(imap,L"user@example.com",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  The PFX password is not hard-coded in source.  Insert code here to obtain it from an
    //  interactive prompt, environment variable, or a secrets vault.

    //  Build a certificate vault and add a PFX to it.
    vault = CkXmlCertVaultW_Create();
    success = CkXmlCertVaultW_AddPfxFile(vault,L"qa_data/smime.pfx",pfxPassword);
    if (success == FALSE) {
        wprintf(L"%s\n",CkXmlCertVaultW_lastErrorText(vault));
        CkImapW_Dispose(imap);
        CkXmlCertVaultW_Dispose(vault);
        return;
    }

    //  Associate the vault with the Imap object.  Only one vault is associated at a time.
    success = CkImapW_UseCertVault(imap,vault);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkXmlCertVaultW_Dispose(vault);
        return;
    }

    success = CkImapW_Disconnect(imap);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkXmlCertVaultW_Dispose(vault);
        return;
    }



    CkImapW_Dispose(imap);
    CkXmlCertVaultW_Dispose(vault);

    }