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

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-mail-token.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read gmail-mail-token.json from the private working directory." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("An OAuth access token is required. Authorize or refresh the account first." & @CRLF)
    Exit
EndIf

Local $sMailbox = "user@example.com"
$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.AuthMethod = "XOAUTH2"
$bSuccess = $oImap.Connect("imap.gmail.com")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not establish the Gmail IMAP TLS connection." & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login($sMailbox,$oToken.StringOf("access_token"))
If ($bSuccess <> True) Then
    $oImap.Disconnect()
    ConsoleWrite("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy." & @CRLF)
    Exit
EndIf

Local $sCapabilities = $oImap.Capability()
If (($oImap.LastMethodSuccess <> True) Or ($oImap.HasCapability("MOVE",$sCapabilities) <> True)) Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("This example requires the MOVE capability." & @CRLF)
    Exit
EndIf

;  These existing custom-label folders are inputs. The operation moves matching messages.
Local $sourceFolder = "Example"
Local $sDestinationFolder = "Example/Processed"
$bSuccess = $oImap.SelectMailbox($sourceFolder)
If ($bSuccess <> True) Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("Could not select the source folder." & @CRLF)
    Exit
EndIf

$oSelected = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("FROM sender@example.net",True,$oSelected)
If ($bSuccess <> True) Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("Could not search the source folder." & @CRLF)
    Exit
EndIf

If ($oSelected.Count = 0) Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("No matching messages; nothing was moved." & @CRLF)
    Exit
EndIf

ConsoleWrite("Moving UID set: " & $oSelected.ToCompactString() & @CRLF)
$bSuccess = $oImap.MoveMessages($oSelected,$sDestinationFolder)
If ($bSuccess <> True) Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("Move failed. Verify the outcome before retrying an interrupted operation." & @CRLF)
    Exit
EndIf

ConsoleWrite("MOVE completed for the selected messages." & @CRLF)
$oImap.Logout()
$oImap.Disconnect()