Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Fetch a MessageSet into an EmailBundle
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.
Background: This is the natural pairing with
QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.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.EmailBundle,
Chilkat.Imap,
Chilkat.Email,
Chilkat.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
msgSet: TMessageSet;
bundle: TEmailBundle;
n: Integer;
email: TEmail;
i: Integer;
begin
success := False;
// Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
// MessageSet and appends them to an EmailBundle. The 1st argument is headersOnly, the 2nd
// is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).
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 download (here, the unseen messages by UID).
msgSet := TMessageSet.Create;
success := imap.QueryMbx('UNSEEN',True,msgSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Download the messages in the set (full bodies) into a bundle.
bundle := TEmailBundle.Create;
success := imap.FetchMsgSet(False,msgSet,bundle);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
n := bundle.MessageCount;
WriteLn('Fetched ' + n + ' messages.');
email := TEmail.Create;
for i := 0 to n - 1 do
begin
success := bundle.EmailAt(i,email);
WriteLn(email.Subject);
end;
success := imap.Disconnect();
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
imap.Free;
msgSet.Free;
bundle.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.