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

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.

email = chilkat2.Email()
success = email.LoadEml("message.eml")
if (success != True):
    print(email.LastErrorText)
    sys.exit()

# Use the recipient's public encryption certificate. Its private key stays with the recipient.
cert = chilkat2.Cert()
success = cert.LoadFromFile("recipient.cer")
if (success != True):
    print(cert.LastErrorText)
    sys.exit()

success = email.SetEncryptCert(cert)
if (success != True):
    print(email.LastErrorText)
    sys.exit()

email.Pkcs7CryptAlg = "aes-gcm"
email.Pkcs7KeyLength = 256
email.OaepPadding = True
email.OaepHash = "sha256"
email.OaepMgfHash = "sha256"
email.SendEncrypted = True
mail = chilkat2.MailMan()
rendered = chilkat2.BinData()
success = mail.RenderToMimeBd(email,rendered)
if (success != True):
    print(mail.LastErrorText)
    sys.exit()

success = rendered.WriteFile("encrypted.eml")
if (success != True):
    print("Failed: rendered.WriteFile.")
    sys.exit()

print("Saved encrypted.eml.")