Lua
Lua
Determine the Number of Unseen Email Messages
Demonstrates how to determine how many unseen messages exist in an email account on an IMAP 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
-- After selecting the mailbox. the total number of emails
-- is immediately available:
local totalNum = imap.num_messages
print("Num messages = " .. totalNum)
-- To determine the number of unseen messages, a call
-- to Search is required, which returns the set of UIDs
-- of all unseen messages.
-- We can choose to fetch UIDs or sequence numbers.
local fetchUids = true
local messageSet = chilkat.MessageSet.new()
success = imap:query_mbx("UNSEEN", fetchUids, messageSet)
if not success then
print(imap.last_error_text)
return
end
local numUnseen = messageSet.count
print(numUnseen)
print("UIDs ----")
-- Display the UIDs
local i = 0
while i < messageSet.count do
print(messageSet:get_id(i))
i = i + 1
end
-- Disconnect from the IMAP server.
success = imap:disconnect()