Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Move IMAP Messages with OAuth2 and the MOVE Extension

See more IMAP Examples

Move matching messages between two existing mailboxes using QueryMbx and MoveMessages. Gmail OAuth2 supplies the concrete connection example. Other servers require their own authentication settings.

This changes all matching messages in the source folder. Choose the source, destination, and search criterion before running it. Gmail custom folders represent labels; moving between them changes label membership. The code checks MOVE at runtime, handles an empty search, and uses UID-based message sets. It does not fall back to copy/delete/EXPUNGE or print authentication logs. Confirm the result before retrying after a lost connection.

First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local token = chilkat.JsonObject.new()
local success = token:load_file("gmail-mail-token.json")
if not success then
    print("Could not read gmail-mail-token.json from the private working directory.")
    return
end

if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
    print("An OAuth access token is required. Authorize or refresh the account first.")
    return
end

local mailbox = "user@example.com"
local imap = chilkat.Imap.new()
imap.ssl = true
imap.port = 993
imap.require_ssl_cert_verify = true
imap.auth_method = "XOAUTH2"
success = imap:connect("imap.gmail.com")
if not success then
    print("Could not establish the Gmail IMAP TLS connection.")
    return
end

success = imap:login(mailbox, token:string_of("access_token"))
if not success then
    imap:disconnect()
    print("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
    return
end

local capabilities = imap:capability()
if (not imap.last_method_success) or (not imap:has_capability("MOVE", capabilities)) then
    imap:logout()
    imap:disconnect()
    print("This example requires the MOVE capability.")
    return
end

-- These existing custom-label folders are inputs. The operation moves matching messages.
local sourceFolder = "Example"
local destinationFolder = "Example/Processed"
success = imap:select_mailbox(sourceFolder)
if not success then
    imap:logout()
    imap:disconnect()
    print("Could not select the source folder.")
    return
end

local selected = chilkat.MessageSet.new()
success = imap:query_mbx("FROM sender@example.net", true, selected)
if not success then
    imap:logout()
    imap:disconnect()
    print("Could not search the source folder.")
    return
end

if selected.count == 0 then
    imap:logout()
    imap:disconnect()
    print("No matching messages; nothing was moved.")
    return
end

print("Moving UID set: " .. selected:to_compact_string())
success = imap:move_messages(selected, destinationFolder)
if not success then
    imap:logout()
    imap:disconnect()
    print("Move failed. Verify the outcome before retrying an interrupted operation.")
    return
end

print("MOVE completed for the selected messages.")
imap:logout()
imap:disconnect()