Sample code for 30+ languages & platforms
Lua

Close the Selected IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.CloseMailbox method, which closes the currently selected mailbox while keeping the authenticated IMAP connection open. This example selects the Inbox and then closes it. (The mailbox-name argument is retained for backward compatibility but is ignored.)

Background: IMAP's CLOSE command returns the session to the "authenticated but no mailbox selected" state without logging out — so you can move on to another mailbox on the same connection. As a side effect, CLOSE also silently expunges \Deleted messages. When you want to switch mailboxes but not expunge, simply select the next mailbox instead of closing.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Imap.CloseMailbox method, which closes the currently selected mailbox
-- while keeping the authenticated IMAP connection open.

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

-- Select a mailbox and work with its messages.
success = imap:select_mailbox("Inbox")
if not success then
    print(imap.last_error_text)
    return
end

-- Close the selected mailbox (the connection stays open and authenticated).
success = imap:close_mailbox("Inbox")
if not success then
    print(imap.last_error_text)
    return
end

print("Mailbox closed; still connected.")

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