Sample code for 30+ languages & platforms
Lua

Expunge Deleted Messages and Close the Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.ExpungeAndClose method, which permanently removes all messages marked with \Deleted from the selected mailbox and then closes the mailbox. This example marks a message deleted and then expunges-and-closes.

Background: This combines two actions into one call: the permanent removal of \Deleted messages (see Expunge) followed by closing the mailbox (see CloseMailbox), returning the session to the authenticated-but-no-mailbox-selected state while staying connected. It's the natural way to finish working with a mailbox after cleaning it up, before selecting another or logging out.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Imap.ExpungeAndClose method, which permanently removes all messages
-- marked with \Deleted from the selected mailbox and then closes the mailbox.

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

-- Mark message (sequence number 1) with the \Deleted flag.
success = imap:set_flag(1, false, "\\Deleted", 1)
if not success then
    print(imap.last_error_text)
    return
end

-- Remove the \Deleted messages and close the mailbox in one step.
success = imap:expunge_and_close()
if not success then
    print(imap.last_error_text)
    return
end

print("Expunged and closed the mailbox.")

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