Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
How to Download Messages in MessageSet One at a Time
See more IMAP Examples
If a message set contains a huge number of emails, it's NOT a good idea to try to download all at once into an email bundle using a method such as FetchBundle. It's better to iterate over the messages in the set to download one by one.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.Email,
Chilkat.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
fetchUids: Boolean;
messageSet: TMessageSet;
email: TEmail;
i: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap := TImap.Create;
// Connect using TLS.
imap.Ssl := True;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Authenticate
success := imap.Login('email_account_login','email_account_password');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Select an IMAP mailbox
success := imap.SelectMailbox('Inbox');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Search for messages and return a set of matching messages.
// (This example will simply search for ALL messages.)
fetchUids := True;
messageSet := TMessageSet.Create;
success := imap.QueryMbx('ALL',fetchUids,messageSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
WriteLn('Number of messages = ' + messageSet.Count);
email := TEmail.Create;
i := 0;
while i < messageSet.Count do
begin
success := imap.FetchEmail(False,messageSet.GetId(i),fetchUids,email);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
WriteLn(email.From + '; ' + email.Subject);
i := i + 1;
end;
WriteLn('OK');
imap.Free;
messageSet.Free;
email.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.