Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Copy a Range of IMAP Messages
See more IMAP Examples
Demonstrates the Chilkat Imap.CopySequence method, which copies a contiguous range of messages, identified by sequence number, from the selected mailbox to a destination mailbox. The first argument is the starting sequence number and the second is the number of messages to copy. This example copies the first ten messages to another mailbox.
Background: Copying a whole range in a single command is far more efficient than copying messages one at a time — it's one server round trip instead of many. This method works by sequence number (position), so it is well suited to "copy the first N" or "copy messages 50–100" style operations. To copy an arbitrary, non-contiguous set of messages, build a
MessageSet and use CopyMultiple.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;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
begin
success := False;
// Demonstrates the Imap.CopySequence method, which copies a contiguous range of messages,
// identified by sequence number, from the selected mailbox to a destination mailbox. The
// 1st argument is the first sequence number and the 2nd is the number of messages to copy.
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;
// Copy the first 10 messages (sequence numbers 1 through 10) to "Archive2026".
success := imap.CopySequence(1,10,'Archive2026');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
WriteLn('Copied the message range.');
success := imap.Disconnect();
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
imap.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.