Lua
Lua
Copy Multiple IMAP Messages to Another Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.CopyMultiple method, which copies the messages in a MessageSet to another mailbox, leaving the originals in place. The first argument is the MessageSet and the second is the destination mailbox name. This example searches for recent messages and copies them to a Backup mailbox.
Background: This maps to the IMAP
COPY command, which duplicates messages entirely on the server — nothing is downloaded. The copy in the destination mailbox is an independent message with its own UID; changing flags on one copy does not affect the other. Combine CopyMultiple with a search from QueryMbx to snapshot or archive matching messages without removing them from their current folder.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Demonstrates the Imap.CopyMultiple method, which copies the messages in a MessageSet to
-- another mailbox, leaving the originals in place. The 1st argument is the MessageSet and
-- the 2nd is the destination mailbox name.
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
-- Find the messages to copy (here, messages seen today, by UID).
local msgSet = chilkat.MessageSet.new()
success = imap:query_mbx("SINCE 15-Jul-2026", true, msgSet)
if not success then
print(imap.last_error_text)
return
end
-- Copy them to the "Backup" mailbox. The originals remain in the Inbox.
success = imap:copy_multiple(msgSet, "Backup")
if not success then
print(imap.last_error_text)
return
end
print("Copied " .. msgSet.count .. " messages to Backup.")
success = imap:disconnect()
if not success then
print(imap.last_error_text)
return
end