Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.0.0+

Create an encrypted S/MIME file without SMTP

See more Email Object Examples

Encrypt a saved draft for a recipient with an RSA encryption certificate. This example uses AES-256-GCM for the content and RSA-OAEP for the content-encryption key. Confirm these choices are supported by the receiving mail client. RenderToMimeBd produces the protected bytes without submitting mail.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkEmailW.h>
#include <CkCertW.h>
#include <CkMailManW.h>
#include <CkBinDataW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkEmailW email;
    success = email.LoadEml(L"message.eml");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    //  Use the recipient's public encryption certificate. Its private key stays with the recipient.
    CkCertW cert;
    success = cert.LoadFromFile(L"recipient.cer");
    if (success != true) {
        wprintf(L"%s\n",cert.lastErrorText());
        return;
    }

    success = email.SetEncryptCert(cert);
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    email.put_Pkcs7CryptAlg(L"aes-gcm");
    email.put_Pkcs7KeyLength(256);
    email.put_OaepPadding(true);
    email.put_OaepHash(L"sha256");
    email.put_OaepMgfHash(L"sha256");
    email.put_SendEncrypted(true);
    CkMailManW mail;
    CkBinDataW rendered;
    success = mail.RenderToMimeBd(email,rendered);
    if (success != true) {
        wprintf(L"%s\n",mail.lastErrorText());
        return;
    }

    success = rendered.WriteFile(L"encrypted.eml");
    if (success != true) {
        wprintf(L"Failed: rendered.WriteFile.\n");
        return;
    }

    wprintf(L"Saved encrypted.eml.\n");
    }