Lua
Lua
Fetch a MessageSet into an EmailBundle
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.
Background: This is the natural pairing with
QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
-- MessageSet and appends them to an EmailBundle. The 1st argument is headersOnly, the 2nd
-- is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).
local imap = chilkat.Imap.new()
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("user@example.com", "myPassword")
if not success then
print(imap.last_error_text)
return
end
success = imap:select_mailbox("Inbox")
if not success then
print(imap.last_error_text)
return
end
-- Find the messages to download (here, the unseen messages by UID).
local msgSet = chilkat.MessageSet.new()
success = imap:query_mbx("UNSEEN", true, msgSet)
if not success then
print(imap.last_error_text)
return
end
-- Download the messages in the set (full bodies) into a bundle.
local bundle = chilkat.EmailBundle.new()
success = imap:fetch_msg_set(false, msgSet, bundle)
if not success then
print(imap.last_error_text)
return
end
local n = bundle.message_count
print("Fetched " .. n .. " messages.")
local email = chilkat.Email.new()
for i = 0, n - 1 do
success = bundle:email_at(i, email)
print(email.subject)
end
success = imap:disconnect()
if not success then
print(imap.last_error_text)
return
end