Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Fetch a Chunk of Messages, Tracking Failures
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchChunk2 method, which downloads a run of messages by sequence number and reports which succeeded and which failed. The arguments are the starting sequence number, the count, a MessageSet that receives the failed IDs, a MessageSet that receives the successfully-fetched IDs, and the EmailBundle. This example fetches the first chunk of messages and reports the success and failure counts.
Background: When bulk-downloading a large mailbox, individual messages can occasionally fail to fetch (a corrupt message on the server, a transient error) without failing the whole operation.
FetchChunk2 is designed for that reality: it returns a partial EmailBundle plus two MessageSet objects so your code can log or retry exactly the messages that did not come through, rather than aborting the entire sync.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.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
numMessages: Integer;
count: Integer;
failedSet: TMessageSet;
fetchedSet: TMessageSet;
bundle: TEmailBundle;
begin
success := False;
// Demonstrates the Imap.FetchChunk2 method, which downloads a run of messages by sequence
// number and reports which succeeded and which failed. The 1st argument is the starting
// sequence number, the 2nd is the count, the 3rd receives the failed IDs, the 4th receives
// the successfully-fetched IDs, and the 5th is the EmailBundle.
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;
numMessages := imap.SelectMailbox('Inbox');
if (numMessages < 0) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Fetch messages 1..25 (or fewer if the mailbox is smaller).
count := 25;
if (count > numMessages) then
begin
count := numMessages;
end;
failedSet := TMessageSet.Create;
fetchedSet := TMessageSet.Create;
bundle := TEmailBundle.Create;
success := imap.FetchChunk2(1,count,failedSet,fetchedSet,bundle);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
WriteLn('Fetched: ' + fetchedSet.Count);
WriteLn('Failed: ' + failedSet.Count);
success := imap.Disconnect();
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
imap.Free;
failedSet.Free;
fetchedSet.Free;
bundle.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.