Sample code for 30+ languages & platforms
Lua

Get the Total Size of an IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailSize method, which returns the complete server-reported size of a message in bytes, including attachment data. The only argument is the Email. This example fetches only the headers and prints the full message size.

Background: The server reports each message's total size, so this value is available even when only the headers were downloaded. That lets a client show a size column in a message list, sort by size, or warn the user before downloading an unusually large message — all without fetching the full body first.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Imap.GetMailSize method, which returns the complete server-reported size
-- of a message in bytes, including attachments.  The only argument is the Email.

local imap = chilkat.Imap.new()

imap.ssl = true
imap.port = 993

local success = imap:connect("imap.example.com")
if not success then
    print(imap.last_error_text)
    return
end

success = imap:login("user@example.com", "myPassword")
if not success then
    print(imap.last_error_text)
    return
end

success = imap:select_mailbox("Inbox")
if not success then
    print(imap.last_error_text)
    return
end

-- Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
-- metadata describing the attachments is present, so the attachment info methods still work.
local headersOnly = true
local useUid = false
local seqNum = 1
local email = chilkat.Email.new()
success = imap:fetch_email(headersOnly, seqNum, useUid, email)
if not success then
    print(imap.last_error_text)
    return
end

local sizeBytes = imap:get_mail_size(email)
print("Total message size: " .. sizeBytes .. " bytes")

success = imap:disconnect()
if not success then
    print(imap.last_error_text)
    return
end