Sample code for 30+ languages & platforms
JavaScript 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.

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var email = new CkEmail();
success = email.LoadEml("message.eml");
if (success !== true) {
    console.log(email.LastErrorText);
    return;
}

//  Use the recipient's public encryption certificate. Its private key stays with the recipient.
var cert = new CkCert();
success = cert.LoadFromFile("recipient.cer");
if (success !== true) {
    console.log(cert.LastErrorText);
    return;
}

success = email.SetEncryptCert(cert);
if (success !== true) {
    console.log(email.LastErrorText);
    return;
}

email.Pkcs7CryptAlg = "aes-gcm";
email.Pkcs7KeyLength = 256;
email.OaepPadding = true;
email.OaepHash = "sha256";
email.OaepMgfHash = "sha256";
email.SendEncrypted = true;
var mail = new CkMailMan();
var rendered = new CkBinData();
success = mail.RenderToMimeBd(email,rendered);
if (success !== true) {
    console.log(mail.LastErrorText);
    return;
}

success = rendered.WriteFile("encrypted.eml");
if (success !== true) {
    console.log("Failed: rendered.WriteFile.");
    return;
}

console.log("Saved encrypted.eml.");