Sample code for 30+ languages & platforms
Lua

Fetch a Single Message's MIME into a StringBuilder

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a StringBuilder. The first argument is the message id, the second (bUid) selects UID vs sequence number, and the third is the StringBuilder, which is cleared first. This example downloads a message by UID and prints the MIME length.

Background: Chilkat interprets the received MIME as UTF-8. Messages using Base64 or quoted-printable transfer encoding are safe because their encoded bytes are ASCII. Raw 8bit or binary content, or unencoded text in a charset such as ISO-8859-1 or Shift_JIS, can be misinterpreted — in those cases use FetchSingleBd to get the exact bytes.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a
-- StringBuilder.  The 1st argument is the message id, the 2nd (bUid) selects UID vs sequence
-- number, and the 3rd is the StringBuilder (cleared first).

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

-- Search for the message ids to download, returning UIDs.
local wantUids = true
local msgSet = chilkat.MessageSet.new()
success = imap:query_mbx("ALL", wantUids, msgSet)
if not success then
    print(imap.last_error_text)
    return
end

if msgSet.count > 0 then
    local uid = msgSet:get_id(0)

    -- Download the message's MIME into a StringBuilder.  The id is a UID.
    local useUid = true
    local sbMime = chilkat.StringBuilder.new()
    success = imap:fetch_single_as_mime_sb(uid, useUid, sbMime)
    if not success then
        print(imap.last_error_text)
        return
    end

    print("MIME length: " .. sbMime.length .. " chars")
end

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