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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
    outFile.WriteLine("Could not read gmail-mail-token.json from the private working directory.")
    WScript.Quit
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
    outFile.WriteLine("An OAuth access token is required. Authorize or refresh the account first.")
    WScript.Quit
End If

mailbox = "user@example.com"
set imap = CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> 1) Then
    outFile.WriteLine("Could not establish the Gmail IMAP TLS connection.")
    WScript.Quit
End If

success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> 1) Then
    success = imap.Disconnect()
    outFile.WriteLine("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
    WScript.Quit
End If

capabilities = imap.Capability()
If ((imap.LastMethodSuccess <> 1) Or (imap.HasCapability("MOVE",capabilities) <> 1)) Then
    success = imap.Logout()
    success = imap.Disconnect()
    outFile.WriteLine("This example requires the MOVE capability.")
    WScript.Quit
End If

'  These existing custom-label folders are inputs. The operation moves matching messages.
sourceFolder = "Example"
destinationFolder = "Example/Processed"
success = imap.SelectMailbox(sourceFolder)
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    outFile.WriteLine("Could not select the source folder.")
    WScript.Quit
End If

set selected = CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx("FROM sender@example.net",1,selected)
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    outFile.WriteLine("Could not search the source folder.")
    WScript.Quit
End If

If (selected.Count = 0) Then
    success = imap.Logout()
    success = imap.Disconnect()
    outFile.WriteLine("No matching messages; nothing was moved.")
    WScript.Quit
End If

outFile.WriteLine("Moving UID set: " & selected.ToCompactString())
success = imap.MoveMessages(selected,destinationFolder)
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    outFile.WriteLine("Move failed. Verify the outcome before retrying an interrupted operation.")
    WScript.Quit
End If

outFile.WriteLine("MOVE completed for the selected messages.")
success = imap.Logout()
success = imap.Disconnect()

outFile.Close