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

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

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("myLogin", "myPassword")
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

-- The NumMessages property contains the number of messages in the selected mailbox.
local numMessages = imap.num_messages
if numMessages == 0 then
    print("No messages exist in the Inbox.")
    return
end

local sbMime = chilkat.StringBuilder.new()
for seqNum = 1, numMessages do
    sbMime:clear()
    success = imap:fetch_single_as_mime_sb(seqNum, false, sbMime)
    if not success then
        print(imap.last_error_text)
        return
    end

    -- The MIME source of the downloaded email is contained in sbMime.
end

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