Lua Requires Chilkat v11.0.0+
Lua
Fetch Single Email by UID or Sequence Number
Assuming the UID is known, download a single email by UID from an IMAP mail server.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
-- Login
success = imap:login("***", "***")
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
local email = chilkat.Email.new()
local uid = 2014
local isUid = true
success = imap:fetch_email(false, uid, isUid, email)
if not success then
print(imap.last_error_text)
return
end
-- Display the From and Subject
print(email.from_address)
print(email.subject)
-- Display the Body property, which is the default body.
-- If an email has an HTML body, the Body property contains
-- the HTML source. Otherwise it contains the plain-text
-- body.
print("---- EMAIL BODY ----")
print(email.body)
print("--------------------")
-- Display the recipients:
for j = 0, email.num_to - 1 do
print(email:get_to_name(j) .. ", " .. email:get_to_addr(j))
end
for j = 0, email.num_cc - 1 do
print(email:get_cc_name(j) .. ", " .. email:get_cc_addr(j))
end
-- Show the total size of the email, including body and attachments:
print(email.size)
-- When a full email is downloaded, we would use the
-- email.NumAttachments property in conjunction with the
-- email.GetMailAttach* methods.
-- However, when an email object contains only the header,
-- we need to access the attachment info differently:
local numAttach = imap:get_mail_num_attach(email)
print(numAttach)
for j = 0, numAttach - 1 do
print(imap:get_mail_attach_filename(email, j))
local attachSize = imap:get_mail_attach_size(email, j)
print(" size = " .. attachSize .. " bytes")
end
print("--")
-- Disconnect from the IMAP server.
success = imap:disconnect()