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 B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim token As ChilkatJsonObject
token.Initialize
success = token.LoadFile("gmail-mail-token.json")
If success <> True Then
Log("Could not read gmail-mail-token.json from the private working directory.")
Return
End If
If (token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token", "", True) = True) Then
Log("An OAuth access token is required. Authorize or refresh the account first.")
Return
End If
Dim mailbox As String = "user@example.com"
Dim imap As ChilkatImap
imap.Initialize("imap")
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If success <> True Then
Log("Could not establish the Gmail IMAP TLS connection.")
Return
End If
success = imap.Login(mailbox, token.StringOf("access_token"))
If success <> True Then
imap.Disconnect
Log("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
Return
End If
Dim capabilities As String = imap.Capability
If (imap.LastMethodSuccess <> True) Or (imap.HasCapability("MOVE", capabilities) <> True) Then
imap.Logout
imap.Disconnect
Log("This example requires the MOVE capability.")
Return
End If
' These existing custom-label folders are inputs. The operation moves matching messages.
Dim sourceFolder As String = "Example"
Dim destinationFolder As String = "Example/Processed"
success = imap.SelectMailbox(sourceFolder)
If success <> True Then
imap.Logout
imap.Disconnect
Log("Could not select the source folder.")
Return
End If
Dim selected As ChilkatMessageSet
selected.Initialize
success = imap.QueryMbx("FROM sender@example.net", True, selected)
If success <> True Then
imap.Logout
imap.Disconnect
Log("Could not search the source folder.")
Return
End If
If selected.Count = 0 Then
imap.Logout
imap.Disconnect
Log("No matching messages; nothing was moved.")
Return
End If
Log("Moving UID set: " & selected.ToCompactString)
success = imap.MoveMessages(selected, destinationFolder)
If success <> True Then
imap.Logout
imap.Disconnect
Log("Move failed. Verify the outcome before retrying an interrupted operation.")
Return
End If
Log("MOVE completed for the selected messages.")
imap.Logout
imap.Disconnect