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

Ruby
require 'chilkat'

success = false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

email = Chilkat::CkEmail.new()
success = email.LoadEml("message.eml")
if (success != true)
    print email.lastErrorText() + "\n";
    exit
end

#  Use the recipient's public encryption certificate. Its private key stays with the recipient.
cert = Chilkat::CkCert.new()
success = cert.LoadFromFile("recipient.cer")
if (success != true)
    print cert.lastErrorText() + "\n";
    exit
end

success = email.SetEncryptCert(cert)
if (success != true)
    print email.lastErrorText() + "\n";
    exit
end

email.put_Pkcs7CryptAlg("aes-gcm")
email.put_Pkcs7KeyLength(256)
email.put_OaepPadding(true)
email.put_OaepHash("sha256")
email.put_OaepMgfHash("sha256")
email.put_SendEncrypted(true)
mail = Chilkat::CkMailMan.new()
rendered = Chilkat::CkBinData.new()
success = mail.RenderToMimeBd(email,rendered)
if (success != true)
    print mail.lastErrorText() + "\n";
    exit
end

success = rendered.WriteFile("encrypted.eml")
if (success != true)
    print "Failed: rendered.WriteFile." + "\n";
    exit
end

print "Saved encrypted.eml." + "\n";