Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Copy Multiple IMAP Messages to Another Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.CopyMultiple method, which copies the messages in a MessageSet to another mailbox, leaving the originals in place. The first argument is the MessageSet and the second is the destination mailbox name. This example searches for recent messages and copies them to a Backup mailbox.
Background: This maps to the IMAP
COPY command, which duplicates messages entirely on the server — nothing is downloaded. The copy in the destination mailbox is an independent message with its own UID; changing flags on one copy does not affect the other. Combine CopyMultiple with a search from QueryMbx to snapshot or archive matching messages without removing them from their current folder.Chilkat Pascal (Lazarus/Delphi) 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.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
msgSet: TMessageSet;
begin
success := False;
// Demonstrates the Imap.CopyMultiple method, which copies the messages in a MessageSet to
// another mailbox, leaving the originals in place. The 1st argument is the MessageSet and
// the 2nd is the destination mailbox name.
imap := TImap.Create;
imap.Ssl := True;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
success := imap.Login('user@example.com','myPassword');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
success := imap.SelectMailbox('Inbox');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Find the messages to copy (here, messages seen today, by UID).
msgSet := TMessageSet.Create;
success := imap.QueryMbx('SINCE 15-Jul-2026',True,msgSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Copy them to the "Backup" mailbox. The originals remain in the Inbox.
success := imap.CopyMultiple(msgSet,'Backup');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
WriteLn('Copied ' + msgSet.Count + ' messages to Backup.');
success := imap.Disconnect();
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
imap.Free;
msgSet.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.