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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
string ls_Mailbox
oleobject loo_Imap
string ls_Capabilities
string ls_SourceFolder
string ls_DestinationFolder
oleobject loo_Selected
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Token.LoadFile("gmail-mail-token.json")
if li_Success <> 1 then
Write-Debug "Could not read gmail-mail-token.json from the private working directory."
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
Write-Debug "An OAuth access token is required. Authorize or refresh the account first."
destroy loo_Token
return
end if
ls_Mailbox = "user@example.com"
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.AuthMethod = "XOAUTH2"
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
Write-Debug "Could not establish the Gmail IMAP TLS connection."
destroy loo_Token
destroy loo_Imap
return
end if
li_Success = loo_Imap.Login(ls_Mailbox,loo_Token.StringOf("access_token"))
if li_Success <> 1 then
loo_Imap.Disconnect()
Write-Debug "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
destroy loo_Token
destroy loo_Imap
return
end if
ls_Capabilities = loo_Imap.Capability()
if (loo_Imap.LastMethodSuccess <> 1) OR (loo_Imap.HasCapability("MOVE",ls_Capabilities) <> 1) then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "This example requires the MOVE capability."
destroy loo_Token
destroy loo_Imap
return
end if
// These existing custom-label folders are inputs. The operation moves matching messages.
ls_SourceFolder = "Example"
ls_DestinationFolder = "Example/Processed"
li_Success = loo_Imap.SelectMailbox(ls_SourceFolder)
if li_Success <> 1 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "Could not select the source folder."
destroy loo_Token
destroy loo_Imap
return
end if
loo_Selected = create oleobject
li_rc = loo_Selected.ConnectToNewObject("Chilkat.MessageSet")
li_Success = loo_Imap.QueryMbx("FROM sender@example.net",1,loo_Selected)
if li_Success <> 1 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "Could not search the source folder."
destroy loo_Token
destroy loo_Imap
destroy loo_Selected
return
end if
if loo_Selected.Count = 0 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "No matching messages; nothing was moved."
destroy loo_Token
destroy loo_Imap
destroy loo_Selected
return
end if
Write-Debug "Moving UID set: " + loo_Selected.ToCompactString()
li_Success = loo_Imap.MoveMessages(loo_Selected,ls_DestinationFolder)
if li_Success <> 1 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "Move failed. Verify the outcome before retrying an interrupted operation."
destroy loo_Token
destroy loo_Imap
destroy loo_Selected
return
end if
Write-Debug "MOVE completed for the selected messages."
loo_Imap.Logout()
loo_Imap.Disconnect()
destroy loo_Token
destroy loo_Imap
destroy loo_Selected