Lua Requires Chilkat v11.0.0+
Lua
IMAP Download and Verify Signed (S/MIME) Email
See more IMAP Examples
Demonstrates how to download and verify digitally signed S/MIME email.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local imap = chilkat.Imap.new()
-- Connect to an IMAP server.
-- Use TLS
imap.ssl = true
imap.port = 993
local success = imap:connect("imap.example.com")
if not success then
print(imap.last_error_text)
return
end
success = imap:login("myLogin", "myPassword")
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
-- We can choose to fetch UIDs or sequence numbers.
local fetchUids = true
-- Get the message IDs of all the emails in the mailbox
local messageSet = chilkat.MessageSet.new()
success = imap:query_mbx("ALL", fetchUids, messageSet)
if not success then
print(imap.last_error_text)
return
end
local email = chilkat.Email.new()
local cert = chilkat.Cert.new()
local i = 0
while i < messageSet.count do
local uid = messageSet:get_id(i)
print("uid: " .. uid)
success = imap:fetch_email(false, uid, true, email)
if not success then
print(imap.last_error_text)
return
end
-- The security layers of signed and/or encrypted emails
-- are automatically "unwrapped" when loaded into
-- a Chilkat email object.
-- An application only needs to check to see if an email
-- was received signed or encrypted, and then examine
-- the success/failure. For example:
if email.received_signed then
print("This email was signed.")
-- Check to see if the signatures were verified.
if email.signatures_valid then
print("Digital signature(s) verified.")
print("Signer: " .. email.signed_by)
-- Get the certificate used for signing.
success = email:last_signer_cert(0, cert)
if not success then
print("Failed to get signing certificate object.")
else
print("Signing cert: " .. cert.subject_cn)
end
else
print("Digital signature verification failed.")
end
end
i = i + 1
end
-- Disconnect from the IMAP server.
success = imap:disconnect()