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

Fetch 1st N Headers of Search Results

Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes 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

-- Get the message IDs of all the emails in the mailbox
-- We can choose to fetch UIDs or sequence numbers.
local fetchUids = true
local messageSet = chilkat.MessageSet.new()
success = imap:query_mbx("ALL", fetchUids, messageSet)
if not success then
    print(imap.last_error_text)
    return
end

local numFound = messageSet.count
if numFound == 0 then
    print("No messages found.")
    return
end

-- Get the 1st 10 messages in messageSet.
local upperBound = 10
if numFound < upperBound then
    upperBound = numFound
end

local i = 0
local bUid = messageSet.has_uids
local headerOnly = true
local email = chilkat.Email.new()

while i < upperBound do
    success = imap:fetch_email(headerOnly, messageSet:get_id(i), bUid, email)
    if not success then
        print(imap.last_error_text)
        return
    end

    print(i .. ": " .. email.subject)

    i = i + 1
end

-- Disconnect from the IMAP server.
success = imap:disconnect()