Sample code for 30+ languages & platforms
Unicode C

Specify a Certificate for Encrypted Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddEncryptCert method, which explicitly specifies a certificate to use when sending encrypted email. Call it once per recipient certificate; ClearEncryptCerts clears the list. This example loads a recipient certificate, registers it, and enables encrypted sending.

Background: When encrypting to multiple people, each recipient needs to be able to decrypt with their own private key. S/MIME handles this by encrypting the message's one-time content key separately under each recipient's public certificate and including all of those wrapped keys in the message. AddEncryptCert is how you build that recipient list — one call per certificate — giving explicit control over exactly whose certificates are used rather than relying on automatic lookup.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmailW email;
    HCkCertW cert;

    success = FALSE;

    //  Demonstrates the AddEncryptCert method, which explicitly specifies a certificate for
    //  sending encrypted email.  Call it once per recipient certificate.  Use ClearEncryptCerts
    //  to clear the list.

    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Encrypted email");
    CkEmailW_putBody(email,L"Encrypted to the specified recipient certificate(s).");
    CkEmailW_putFrom(email,L"alice@example.com");
    CkEmailW_AddTo(email,L"Bob",L"bob@example.com");

    //  Load a recipient certificate (public key) and add it to the encryption cert list.
    cert = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert,L"qa_data/certs/recipient.cer");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        return true;
    }

    success = CkEmailW_AddEncryptCert(email,cert);
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        return true;
    }

    //  Request encrypted sending.
    CkEmailW_putSendEncrypted(email,TRUE);

    wprintf(L"Added the recipient's encryption certificate.\n");

    //  Note: The path "qa_data/certs/recipient.cer" is a relative local filesystem path,
    //  relative to the current working directory of the running application.


    CkEmailW_Dispose(email);
    CkCertW_Dispose(cert);

    }