Sample code for 30+ languages & platforms
Unicode C

Set the Recipient Encryption Certificate

See more Email Object Examples

Demonstrates the Chilkat Email.SetEncryptCert method, which sets the explicit recipient encryption certificate for sending an encrypted email. This example loads the recipient's certificate, sets it, and enables encrypted sending.

Background: To encrypt a message for a recipient you need their public certificate — hence a .cer file (no private key) suffices. SetEncryptCert specifies a single recipient certificate; to encrypt for multiple recipients, use AddEncryptCert once per certificate. Either way, setting SendEncrypted to true is what actually requests encryption when the message is sent.

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 SetEncryptCert method, which sets the explicit recipient encryption
    //  certificate for sending an encrypted email.

    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Encrypted email");
    CkEmailW_putBody(email,L"This message will be sent S/MIME encrypted.");
    CkEmailW_putFrom(email,L"alice@example.com");
    CkEmailW_AddTo(email,L"Bob",L"bob@example.com");

    //  Load the recipient's certificate (only the public key is needed to encrypt).
    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;
    }

    //  Set the explicit recipient encryption certificate.
    success = CkEmailW_SetEncryptCert(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"Encryption certificate set.\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);

    }