Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
How to Copy IMAP Mail to another IMAP Server
Demonstrates how to copy the entire contents of an IMAP mailbox to another IMAP server.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.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imapSrc: TImap;
imapDest: TImap;
sbMime: TStringBuilder;
seqNum: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imapSrc := TImap.Create;
// Connect to our source IMAP server.
success := imapSrc.Connect('imap.example.com');
if (success = False) then
begin
WriteLn(imapSrc.LastErrorText);
Exit;
end;
// Login to the source IMAP server
success := imapSrc.Login('admin@chilkatsoft.com','myPassword');
if (success = False) then
begin
WriteLn(imapSrc.LastErrorText);
Exit;
end;
imapDest := TImap.Create;
// Connect to our destination IMAP server.
success := imapDest.Connect('mail.example-code.com');
if (success = False) then
begin
WriteLn(imapDest.LastErrorText);
Exit;
end;
// Login to the destination IMAP server
success := imapDest.Login('myLogin','myPassword');
if (success = False) then
begin
WriteLn(imapDest.LastErrorText);
Exit;
end;
// Select an IMAP mailbox on the source IMAP server
success := imapSrc.SelectMailbox('Inbox');
if (success = False) then
begin
WriteLn(imapSrc.LastErrorText);
Exit;
end;
// After selecting a mailbox, the NumMessages property will
// be updated to reflect the total number of emails in the mailbox:
WriteLn(imapSrc.NumMessages);
// The emails in the mailbox will always have sequence numbers
// ranging from 1 to NumMessages.
// This example will copy the first 10 messages using sequence numbers
sbMime := TStringBuilder.Create;
for seqNum := 1 to 10 do
begin
sbMime.Clear();
success := imapSrc.FetchSingleAsMimeSb(seqNum,False,sbMime);
if (success = False) then
begin
WriteLn(imapSrc.LastErrorText);
Exit;
end;
success := imapDest.AppendMimeWithFlagsSb('Inbox',sbMime,False,False,False,False);
if (success = False) then
begin
WriteLn(imapDest.LastErrorText);
Exit;
end;
end;
// Disconnect from the IMAP servers.
success := imapSrc.Disconnect();
success := imapDest.Disconnect();
imapSrc.Free;
imapDest.Free;
sbMime.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.