Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loToken
LOCAL lcMailbox
LOCAL loImap
LOCAL lcCapabilities
LOCAL lcSourceFolder
LOCAL lcDestinationFolder
LOCAL loSelected

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-mail-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not read gmail-mail-token.json from the private working directory."
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
    ? "An OAuth access token is required. Authorize or refresh the account first."
    RELEASE loToken
    CANCEL
ENDIF

lcMailbox = "user@example.com"
loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
loImap.RequireSslCertVerify = 1
loImap.AuthMethod = "XOAUTH2"
lnSuccess = loImap.Connect("imap.gmail.com")
IF (lnSuccess <> 1) THEN
    ? "Could not establish the Gmail IMAP TLS connection."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login(lcMailbox,loToken.StringOf("access_token"))
IF (lnSuccess <> 1) THEN
    loImap.Disconnect()
    ? "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

lcCapabilities = loImap.Capability()
IF ((loImap.LastMethodSuccess <> 1) OR (loImap.HasCapability("MOVE",lcCapabilities) <> 1)) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "This example requires the MOVE capability."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

*  These existing custom-label folders are inputs. The operation moves matching messages.
lcSourceFolder = "Example"
lcDestinationFolder = "Example/Processed"
lnSuccess = loImap.SelectMailbox(lcSourceFolder)
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Could not select the source folder."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

loSelected = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("FROM sender@example.net",1,loSelected)
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Could not search the source folder."
    RELEASE loToken
    RELEASE loImap
    RELEASE loSelected
    CANCEL
ENDIF

IF (loSelected.Count = 0) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "No matching messages; nothing was moved."
    RELEASE loToken
    RELEASE loImap
    RELEASE loSelected
    CANCEL
ENDIF

? "Moving UID set: " + loSelected.ToCompactString()
lnSuccess = loImap.MoveMessages(loSelected,lcDestinationFolder)
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Move failed. Verify the outcome before retrying an interrupted operation."
    RELEASE loToken
    RELEASE loImap
    RELEASE loSelected
    CANCEL
ENDIF

? "MOVE completed for the selected messages."
loImap.Logout()
loImap.Disconnect()

RELEASE loToken
RELEASE loImap
RELEASE loSelected