Lua Requires Chilkat v11.0.0+
Lua
How to Copy IMAP Mail to another IMAP Server
Demonstrates how to copy the entire contents of an IMAP mailbox to another IMAP server.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local imapSrc = chilkat.Imap.new()
-- Connect to our source IMAP server.
local success = imapSrc:connect("imap.example.com")
if not success then
print(imapSrc.last_error_text)
return
end
-- Login to the source IMAP server
success = imapSrc:login("admin@chilkatsoft.com", "myPassword")
if not success then
print(imapSrc.last_error_text)
return
end
local imapDest = chilkat.Imap.new()
-- Connect to our destination IMAP server.
success = imapDest:connect("mail.example-code.com")
if not success then
print(imapDest.last_error_text)
return
end
-- Login to the destination IMAP server
success = imapDest:login("myLogin", "myPassword")
if not success then
print(imapDest.last_error_text)
return
end
-- Select an IMAP mailbox on the source IMAP server
success = imapSrc:select_mailbox("Inbox")
if not success then
print(imapSrc.last_error_text)
return
end
-- After selecting a mailbox, the NumMessages property will
-- be updated to reflect the total number of emails in the mailbox:
print(imapSrc.num_messages)
-- The emails in the mailbox will always have sequence numbers
-- ranging from 1 to NumMessages.
-- This example will copy the first 10 messages using sequence numbers
local sbMime = chilkat.StringBuilder.new()
for seqNum = 1, 10 do
sbMime:clear()
success = imapSrc:fetch_single_as_mime_sb(seqNum, false, sbMime)
if not success then
print(imapSrc.last_error_text)
return
end
success = imapDest:append_mime_with_flags_sb("Inbox", sbMime, false, false, false, false)
if not success then
print(imapDest.last_error_text)
return
end
end
-- Disconnect from the IMAP servers.
success = imapSrc:disconnect()
success = imapDest:disconnect()