Lua Requires Chilkat v11.0.0+
Lua
Reading Unread POP3 Email
The POP3 protocol cannot determine which emails are "unread," and pure POP3 servers do not store this information. Servers like Exchange Server, offering both POP3 and IMAP interfaces, do contain read/unread data, but it's only accessible through IMAP. Email clients like Outlook and Thunderbird store read/unread statuses on the client side. The example demonstrates using UIDLs to track and manage "unread" emails.Chilkat Lua Downloads
local chilkat = require("chilkat")
local success = false
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- The mailman object is used for receiving (POP3)
-- and sending (SMTP) email.
local mailman = chilkat.MailMan.new()
-- Set the POP3 server's hostname
mailman.mail_host = "pop.example.com"
-- Set the POP3 login/password.
mailman.pop_username = "***"
mailman.pop_password = "***"
-- Keep a records of already-seen UIDLs in hash table serialized to an XML file.
local seenUidlsPath = "c:/temp/seenUidls.xml"
local sbXml = chilkat.StringBuilder.new()
local htSeenUidls = chilkat.Hashtable.new()
local fac = chilkat.FileAccess.new()
if fac:file_exists(seenUidlsPath) then
success = sbXml:load_file(seenUidlsPath, "utf-8")
if not success then
print(sbXml.last_error_text)
return
end
htSeenUidls:add_from_xml_sb(sbXml)
end
-- Get the complete list of UIDLs on the mail server.
local stUidls = chilkat.StringTable.new()
success = mailman:fetch_uidls(stUidls)
if not success then
print(mailman.last_error_text)
return
end
-- Build a list of unseen UIDLs
local stUnseenUidls = chilkat.StringTable.new()
local uidl = ""
local i = 0
local count = stUidls.count
while i < count do
uidl = stUidls:string_at(i)
if not htSeenUidls:contains(uidl) then
stUnseenUidls:append(uidl)
end
i = i + 1
end
if stUnseenUidls.count == 0 then
print("No unseen emails!")
return
end
-- Download the unseen emails, adding each UIDL to the "seen" hash table.
local email = chilkat.Email.new()
count = stUnseenUidls.count
i = 0
while i < count do
-- Download the full email.
uidl = stUnseenUidls:string_at(i)
success = mailman:fetch_by_uidl(uidl, false, 0, email)
if not success then
print(mailman.last_error_text)
return
end
print(i)
print("From: " .. email.from)
print("Subject: " .. email.subject)
-- Add this UIDL to the "seen" hash table.
htSeenUidls:add_str(uidl, "")
i = i + 1
end
mailman:pop3_end_session()
-- Update the "seen" UIDLs file.
sbXml:clear()
htSeenUidls:to_xml_sb(sbXml)
success = sbXml:write_file(seenUidlsPath, "utf-8", false)
if not success then
print(sbXml.last_error_text)
return
end
print("Success.")