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.
Chilkat Ruby Downloads
require 'chilkat'
success = false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
token = Chilkat::CkJsonObject.new()
success = token.LoadFile("gmail-mail-token.json")
if (success != true)
print "Could not read gmail-mail-token.json from the private working directory." + "\n";
exit
end
if ((token.JsonTypeOf("access_token") != 1) or (token.StringOfEquals("access_token","",true) == true))
print "An OAuth access token is required. Authorize or refresh the account first." + "\n";
exit
end
mailbox = "user@example.com"
imap = Chilkat::CkImap.new()
imap.put_Ssl(true)
imap.put_Port(993)
imap.put_RequireSslCertVerify(true)
imap.put_AuthMethod("XOAUTH2")
success = imap.Connect("imap.gmail.com")
if (success != true)
print "Could not establish the Gmail IMAP TLS connection." + "\n";
exit
end
success = imap.Login(mailbox,token.stringOf("access_token"))
if (success != true)
imap.Disconnect()
print "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy." + "\n";
exit
end
capabilities = imap.capability()
if ((imap.get_LastMethodSuccess() != true) or (imap.HasCapability("MOVE",capabilities) != true))
imap.Logout()
imap.Disconnect()
print "This example requires the MOVE capability." + "\n";
exit
end
# These existing custom-label folders are inputs. The operation moves matching messages.
sourceFolder = "Example"
destinationFolder = "Example/Processed"
success = imap.SelectMailbox(sourceFolder)
if (success != true)
imap.Logout()
imap.Disconnect()
print "Could not select the source folder." + "\n";
exit
end
selected = Chilkat::CkMessageSet.new()
success = imap.QueryMbx("FROM sender@example.net",true,selected)
if (success != true)
imap.Logout()
imap.Disconnect()
print "Could not search the source folder." + "\n";
exit
end
if (selected.get_Count() == 0)
imap.Logout()
imap.Disconnect()
print "No matching messages; nothing was moved." + "\n";
exit
end
print "Moving UID set: " + selected.toCompactString() + "\n";
success = imap.MoveMessages(selected,destinationFolder)
if (success != true)
imap.Logout()
imap.Disconnect()
print "Move failed. Verify the outcome before retrying an interrupted operation." + "\n";
exit
end
print "MOVE completed for the selected messages." + "\n";
imap.Logout()
imap.Disconnect()