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

Imap.GetMailSize vs Email.Size

Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.

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

-- When downloading headers, each email object contains
-- (obviously) the headers, but the body will be missing.
-- Attachments will not be included.  However, it is
-- possible to get information about the attachments
-- as well as the complete size of the email.
local bundle = chilkat.EmailBundle.new()
local headersOnly = true
success = imap:fetch_msg_set(headersOnly, messageSet, bundle)
if not success then
    print(imap.last_error_text)
    return
end

-- Loop over the email objects and display information about each:
local email = chilkat.Email.new()
local i = 0
local j = 0
local numEmails = bundle.message_count
while i < numEmails do
    bundle:email_at(i, email)

    -- Display the From and Subject
    print(email.from)
    print(email.subject)

    -- Display the recipients:
    j = 0
    while j < email.num_to do
        print("TO: " .. email:get_to_name(j) .. ", " .. email:get_to_addr(j))
        j = j + 1
    end

    j = 0
    while j < email.num_cc do
        print("CC: " .. email:get_cc_name(j) .. ", " .. email:get_cc_addr(j))
        j = j + 1
    end

    -- Show the total size of the email, including body and attachments:
    print(email.size)

    -- When a full email is downloaded, we would use the
    -- email.NumAttachments property in conjunction with the
    -- email.GetMailAttach* methods.
    -- However, when an email object contains only the header,
    -- we need to access the attachment info differently:
    local numAttach = imap:get_mail_num_attach(email)
    j = 0
    while j < numAttach do
        print(imap:get_mail_attach_filename(email, j))
        local attachSize = imap:get_mail_attach_size(email, j)
        print("    size = " .. attachSize .. " bytes")
        j = j + 1
    end

    print("--")

    i = i + 1
end

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