Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Move IMAP Messages to Another Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.MoveMessages method, which moves the messages in a MessageSet to another mailbox on the server. The first argument is the MessageSet and the second is the destination folder name. This example searches for newsletter messages and moves them to the Archive mailbox.
Background: A true server-side move (the IMAP
MOVE extension) is atomic and efficient: the message never leaves the server, so no download/re-upload is involved. If the server lacks the MOVE capability, the equivalent is copy-then-delete-then-expunge — Chilkat handles that fallback for you, but the destination mailbox must already exist.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.MoveMessages method, which moves the messages in a MessageSet to
// another mailbox on the server. The 1st argument is the MessageSet and the 2nd is the
// destination folder 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 move (here, messages from a particular sender, by UID).
msgSet := TMessageSet.Create;
success := imap.QueryMbx('FROM "newsletter@example.com"',True,msgSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Move them to the "Archive" mailbox.
success := imap.MoveMessages(msgSet,'Archive');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
WriteLn('Moved ' + msgSet.Count + ' messages to Archive.');
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.