Sample code for 30+ languages & platforms
Lua

Find Certificate for Email Encryption

Demonstrates finding the recipient's certificate in the Windows certificate store and using it to send encrypted 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 mailman = chilkat.MailMan.new()

-- Set the SMTP server.
mailman.smtp_host = "smtp.example.com"

-- Create a new email object
local email = chilkat.Email.new()

email.subject = "This email is encrypted"
email.body = "This is a digitally encrypted mail"
email.from = "Joe <joe@example.com>"
-- Emails are encrypted using the recipient's certificate.
local recipientEmailAddr = "jane@example2.com"
email:add_to("Jane", recipientEmailAddr)

-- Indicate that the email is to be sent encrypted.
email.send_encrypted = true

-- This example demonstrates finding the email encryption certificate
-- on a Windows system where the certificate is stored in the Windows
-- certificate store.

local cert = chilkat.Cert.new()
-- The recipient's certificate is used to encrypt.
-- (Because the recipient is the only one in possession of the private key to decrypt.)
local success = cert:load_by_email_address(recipientEmailAddr)
if not success then
    print(cert.last_error_text)
    return
end

-- Specify the certificate to be used for encryption.
success = email:set_encrypt_cert(cert)

success = mailman:send_email(email)
if not success then
    print(mailman.last_error_text)
else
    print("Encrypted Mail Sent!")
end