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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Imap,
Chilkat.JsonObject,
Chilkat.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
token: TJsonObject;
mailbox: string;
imap: TImap;
capabilities: string;
sourceFolder: string;
destinationFolder: string;
selected: TMessageSet;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token := TJsonObject.Create;
success := token.LoadFile('gmail-mail-token.json');
if (success <> True) then
begin
WriteLn('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','',True) = True)) then
begin
WriteLn('An OAuth access token is required. Authorize or refresh the account first.');
Exit;
end;
mailbox := 'user@example.com';
imap := TImap.Create;
imap.Ssl := True;
imap.Port := 993;
imap.RequireSslCertVerify := True;
imap.AuthMethod := 'XOAUTH2';
success := imap.Connect('imap.gmail.com');
if (success <> True) then
begin
WriteLn('Could not establish the Gmail IMAP TLS connection.');
Exit;
end;
success := imap.Login(mailbox,token.StringOf('access_token'));
if (success <> True) then
begin
imap.Disconnect();
WriteLn('IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.');
Exit;
end;
capabilities := imap.Capability();
if ((imap.LastMethodSuccess <> True) or (imap.HasCapability('MOVE',capabilities) <> True)) then
begin
imap.Logout();
imap.Disconnect();
WriteLn('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 <> True) then
begin
imap.Logout();
imap.Disconnect();
WriteLn('Could not select the source folder.');
Exit;
end;
selected := TMessageSet.Create;
success := imap.QueryMbx('FROM sender@example.net',True,selected);
if (success <> True) then
begin
imap.Logout();
imap.Disconnect();
WriteLn('Could not search the source folder.');
Exit;
end;
if (selected.Count = 0) then
begin
imap.Logout();
imap.Disconnect();
WriteLn('No matching messages; nothing was moved.');
Exit;
end;
WriteLn('Moving UID set: ' + selected.ToCompactString());
success := imap.MoveMessages(selected,destinationFolder);
if (success <> True) then
begin
imap.Logout();
imap.Disconnect();
WriteLn('Move failed. Verify the outcome before retrying an interrupted operation.');
Exit;
end;
WriteLn('MOVE completed for the selected messages.');
imap.Logout();
imap.Disconnect();
token.Free;
imap.Free;
selected.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.