Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local mail = chilkat.MailMan.new()
mail.mail_host = "pop.example.com"
mail.mail_port = 995
mail.pop_ssl = true
mail.require_ssl_cert_verify = true
mail.pop_username = "MAIL_USER"
mail.pop_password = "MAIL_PASSWORD"
local previews = chilkat.EmailBundle.new()
-- Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
local success = mail:fetch_range(true, true, 0, 0, 4, previews)
if not success then
    print(mail.last_error_text)
    return
end

if previews.message_count == 0 then
    print("There are no messages to preview.")
    success = mail:pop3_end_session()
    if not success then
        print(mail.last_error_text)
        return
    end

    return
end

local header = chilkat.Email.new()
local i = 0
while i < previews.message_count do
    success = previews:email_at(i, header)
    if not success then
        print(previews.last_error_text)
        return
    end

    print(header.uidl .. "  " .. header.subject)
    i = i + 1
end

-- A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
success = previews:email_at(0, header)
if not success then
    print(previews.last_error_text)
    return
end

local email = chilkat.Email.new()
success = mail:fetch_full(header, email)
if not success then
    print(mail.last_error_text)
    return
end

success = mail:pop3_end_session()
if not success then
    print(mail.last_error_text)
    return
end

print("Full message: " .. email.subject .. "  Files: " .. email.num_attachments)

success = email:save_eml("pop-received.eml")
if not success then
    print(email.last_error_text)
    return
end

print("Saved pop-received.eml.")