Sample code for 30+ languages & platforms
Lua

Refresh an Email Object's IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.RefetchMailFlags method, which updates the flags stored on an Email object to the current values on the server. The only argument is the Email object. This example re-reads the flags for a previously fetched message and then checks its Seen state.

Background: Flags are shared mailbox state, so between the time you fetch a message and the time you act on it, another client (a phone, a webmail tab) may have marked it read, flagged, or deleted. RefetchMailFlags pulls just the flags — not the whole message — so a long-running job can cheaply re-check the live state before deciding what to do, avoiding acting on stale information.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Imap.RefetchMailFlags method, which updates the flags stored on an Email
-- object to the current values on the server.  The only argument is the Email object.

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

local msgSet = chilkat.MessageSet.new()
success = imap:query_mbx("ALL", true, msgSet)
if not success then
    print(imap.last_error_text)
    return
end

local bundle = chilkat.EmailBundle.new()
success = imap:fetch_msg_set(true, msgSet, bundle)
if not success then
    print(imap.last_error_text)
    return
end

local email = chilkat.Email.new()
if bundle.message_count > 0 then
    success = bundle:email_at(0, email)

    -- Re-read the flags from the server, in case another client changed them since the fetch.
    success = imap:refetch_mail_flags(email)
    if not success then
        print(imap.last_error_text)
        return
    end

    local seen = imap:get_mail_flag(email, "Seen")
    print("Current Seen state: " .. seen)
end

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