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

Delphi ActiveX
var
success: Integer;
token: TChilkatJsonObject;
mailbox: WideString;
imap: TChilkatImap;
capabilities: WideString;
sourceFolder: WideString;
destinationFolder: WideString;
selected: TMessageSet;

begin
success := 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

token := TChilkatJsonObject.Create(Self);
success := token.LoadFile('gmail-mail-token.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read gmail-mail-token.json from the private working directory.');
    Exit;
  end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',1) = 1)) then
  begin
    Memo1.Lines.Add('An OAuth access token is required. Authorize or refresh the account first.');
    Exit;
  end;
mailbox := 'user@example.com';
imap := TChilkatImap.Create(Self);
imap.Ssl := 1;
imap.Port := 993;
imap.RequireSslCertVerify := 1;
imap.AuthMethod := 'XOAUTH2';
success := imap.Connect('imap.gmail.com');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not establish the Gmail IMAP TLS connection.');
    Exit;
  end;
success := imap.Login(mailbox,token.StringOf('access_token'));
if (success <> 1) then
  begin
    imap.Disconnect();
    Memo1.Lines.Add('IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.');
    Exit;
  end;
capabilities := imap.Capability();
if ((imap.LastMethodSuccess <> 1) or (imap.HasCapability('MOVE',capabilities) <> 1)) then
  begin
    imap.Logout();
    imap.Disconnect();
    Memo1.Lines.Add('This example requires the MOVE capability.');
    Exit;
  end;
//  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
  begin
    imap.Logout();
    imap.Disconnect();
    Memo1.Lines.Add('Could not select the source folder.');
    Exit;
  end;
selected := TMessageSet.Create(Self);
success := imap.QueryMbx('FROM sender@example.net',1,selected.ControlInterface);
if (success <> 1) then
  begin
    imap.Logout();
    imap.Disconnect();
    Memo1.Lines.Add('Could not search the source folder.');
    Exit;
  end;
if (selected.Count = 0) then
  begin
    imap.Logout();
    imap.Disconnect();
    Memo1.Lines.Add('No matching messages; nothing was moved.');
    Exit;
  end;
Memo1.Lines.Add('Moving UID set: ' + selected.ToCompactString());
success := imap.MoveMessages(selected.ControlInterface,destinationFolder);
if (success <> 1) then
  begin
    imap.Logout();
    imap.Disconnect();
    Memo1.Lines.Add('Move failed. Verify the outcome before retrying an interrupted operation.');
    Exit;
  end;
Memo1.Lines.Add('MOVE completed for the selected messages.');
imap.Logout();
imap.Disconnect();