JavaScript Requires Chilkat v11.0.0+
JavaScript
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.
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.
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
var email = new CkEmail();
success = email.LoadEml("with-attachments.eml");
if (success !== true) {
console.log(email.LastErrorText);
return;
}
var signer = new CkCert();
success = signer.LoadPfxFile("signer.pfx","SIGNER_PASSWORD");
if (success !== true) {
console.log(signer.LastErrorText);
return;
}
var recipient = new CkCert();
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 CkMailMan();
var rendered = new CkBinData();
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.");