Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

IMAP Read Encrypted Email

See more IMAP Examples

Demonstrates how to read encrypted email from an IMAP mailbox.

Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.

Information about the original encrypted state of the email is available after it has been downloaded and decrypted.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local imap = chilkat.Imap.new()

imap.ssl = true
imap.port = 993
local success = imap:connect("imap.example2.com")
if not success then
    print(imap.last_error_text)
    return
end

-- We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager.
-- See how we originally saved the email credentials to the Keychain here:
-- Save Email Credentials in Apple Keychain or Windows Credentials Manager
local secrets = chilkat.Secrets.new()

-- On Windows, this is the Windows Credentials Manager
-- On MacOS/iOS, it is the Apple Keychain
secrets.location = "local_manager"

-- Specify the name of the secret.
-- service and username are required.
-- appName and domain are optional.
-- Note: The values are arbitrary and can be anything you want.
local json = chilkat.JsonObject.new()
json:update_string("appName", "MyEmailApp")
json:update_string("service", "IMAP")
json:update_string("domain", "example2.com")
json:update_string("username", "jane@example2.com")

local password = secrets:get_secret_str(json)
if not secrets.last_method_success then
    print(secrets.last_error_text)
    return
end

success = imap:login("jane@example2.com", password)
if not success then
    print(imap.last_error_text)
    return
end

-- Select an IMAP mailbox
success = imap:select_mailbox("Inbox")
if not success then
    print(imap.last_error_text)
    return
end

-- This example: Send Encrypted Email using Certificate in Apple Keychain
-- sent an email with the subject "This email is encrypted".
-- Let's download an email with the word "encrypted" in the subject.

local messageSet = chilkat.MessageSet.new()
success = imap:query_mbx("SUBJECT encrypted", true, messageSet)
if not success then
    print(imap.last_error_text)
    return
end

if messageSet.count == 0 then
    print("No messages found.")
    return
end

-- Reading encrypted email works the same as reading non-encrypted email.
-- If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store),
-- Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.

local uid = messageSet:get_id(0)

local email = chilkat.Email.new()
success = imap:fetch_email(false, uid, true, email)
if not success then
    print(imap.last_error_text)
    return
end

-- Here we can show if the email was received encrypted, if it was successfully decrypted, and
-- which certificate was used to decrypt.
print("Email received encrypted: " .. tostring(email.received_encrypted))

-- Was it successfully decrypted?
print("Successfully decrypted: " .. tostring(email.decrypted))

-- What cert was used to decrypt?
print("Encrypted by: " .. email.encrypted_by)
local cert = chilkat.Cert.new()
success = email:last_decrypt_cert(cert)
if success then
    print("Certificate DN: " .. cert.subject_dn)
end

-- Show the decrypted email body.
print(email.body)

imap:disconnect()