Sample code for 30+ languages & platforms
Node.js Requires Chilkat v11.0.0+

Sign and encrypt an email with S/MIME

See more Email Object Examples

Use the sender’s signing PFX and the recipient’s public RSA certificate with with-attachments.eml, which has one recipient. Supply your own signing-key password from a credential store. Render to bytes to apply both S/MIME operations without connecting to SMTP.

Chilkat Node.js Downloads

Node.js
NODEJS_PRELUDE

function chilkatExample() {

    var success = false;

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

    var email = new chilkat.Email();
    success = email.LoadEml("with-attachments.eml");
    if (success !== true) {
        console.log(email.LastErrorText);
        return;
    }

    var signer = new chilkat.Cert();
    success = signer.LoadPfxFile("signer.pfx","SIGNER_PASSWORD");
    if (success !== true) {
        console.log(signer.LastErrorText);
        return;
    }

    var recipient = new chilkat.Cert();
    success = recipient.LoadFromFile("recipient.cer");
    if (success !== true) {
        console.log(recipient.LastErrorText);
        return;
    }

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

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

    email.SigningHashAlg = "sha256";
    email.Pkcs7CryptAlg = "aes";
    email.Pkcs7KeyLength = 256;
    email.OaepPadding = true;
    email.OaepHash = "sha256";
    email.OaepMgfHash = "sha256";
    email.SendSigned = true;
    email.SendEncrypted = true;
    var mail = new chilkat.MailMan();
    var rendered = new chilkat.BinData();
    success = mail.RenderToMimeBd(email,rendered);
    if (success !== true) {
        console.log(mail.LastErrorText);
        return;
    }

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

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

}

chilkatExample();