Sample code for 30+ languages & platforms
Lua

S/MIME Encrypt .eml without Sending

See more Email Object Examples

Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the email.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local email = chilkat.Email.new()

local success = email:load_eml("c:/temp/email/unencrypted.eml")
if not success then
    print(email.last_error_text)
    return
end

-- The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
email.pkcs7_crypt_alg = "aes-gcm"
email.pkcs7_key_length = 256
email.oaep_padding = true
email.oaep_hash = "sha256"
email.oaep_mgf_hash = "sha256"

local cert = chilkat.Cert.new()
success = cert:load_from_file("c/temps/cert/recipient.cer")
if not success then
    print(cert.last_error_text)
    return
end

email.send_encrypted = true
email:set_encrypt_cert(cert)

local sbSmime = chilkat.StringBuilder.new()

-- The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
-- No email is sent.
local mailman = chilkat.MailMan.new()
success = mailman:render_to_mime_sb(email, sbSmime)
if not success then
    print(mailman.last_error_text)
    return
end

success = sbSmime:write_file("c:/temp/encryptedEmail.eml", "utf-8", false)
if not success then
    print(mailman.last_error_text)
    return
end

print("Success!")