Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Read IMAP Email Headers
This example demonstrates how to connect to an IMAP server and download only the email headers for all messages in a mailbox. Downloading headers only is much faster and more efficient than downloading complete emails because the message bodies and attachment contents are not retrieved.
The example shows how to:
- Connect to an IMAP server using SSL/TLS
- Authenticate and select a mailbox
- Query for all messages in the mailbox
- Fetch header-only email information
- Display sender, subject, recipients, and total message size
- Retrieve attachment metadata such as filenames and sizes without downloading the actual attachments
This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage.
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;
fetchUids: Boolean;
messageSet: TMessageSet;
bundle: TEmailBundle;
headersOnly: Boolean;
email: TEmail;
name: string;
addr: string;
i: Integer;
j: Integer;
numAttach: Integer;
attachSize: Integer;
begin
success := False;
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for example code.
imap := TImap.Create;
// Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
imap.Ssl := True;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Authenticate with the IMAP server.
success := imap.Login('****','****');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Select the mailbox (folder) to access.
success := imap.SelectMailbox('Inbox');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Get the identifiers for all messages in the selected mailbox.
// Setting fetchUids = cktrue causes IMAP UIDs to be returned.
// If ckfalse, IMAP sequence numbers are returned instead.
fetchUids := True;
messageSet := TMessageSet.Create;
success := imap.QueryMbx('ALL',fetchUids,messageSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Download only the email headers for the messages in the set.
// This is much faster than downloading full emails because the
// message bodies and attachment contents are not retrieved.
//
// Even though the attachments themselves are not downloaded,
// Chilkat can still provide information such as attachment
// filenames, attachment sizes, and the total message size.
bundle := TEmailBundle.Create;
headersOnly := True;
success := imap.FetchMsgSet(headersOnly,messageSet,bundle);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Iterate over each email in the bundle and display summary information.
email := TEmail.Create;
i := 0;
j := 0;
while i < bundle.MessageCount do
begin
bundle.EmailAt(i,email);
// Display the sender and subject line.
WriteLn(email.From);
WriteLn(email.Subject);
// Display all "To" recipients.
j := 0;
while j < email.NumTo do
begin
WriteLn('TO: ' + email.GetToName(j) + ', ' + email.GetToAddr(j));
j := j + 1;
end;
// Display all "CC" recipients.
j := 0;
while j < email.NumCC do
begin
WriteLn('CC: ' + email.GetCcName(j) + ', ' + email.GetCcAddr(j));
j := j + 1;
end;
// Display the total size of the email message, including
// the body and all attachments, even though only headers
// were downloaded.
WriteLn(email.Size);
// When full emails are downloaded, attachment information
// is accessed using the email.NumAttachments property and
// the email.GetMailAttach* methods.
//
// However, because this example downloaded headers only,
// attachment metadata must be obtained using the IMAP object.
numAttach := imap.GetMailNumAttach(email);
j := 0;
while j < numAttach do
begin
// Display the attachment filename.
WriteLn(imap.GetMailAttachFilename(email,j));
// Display the attachment size in bytes.
attachSize := imap.GetMailAttachSize(email,j);
WriteLn(' size = ' + attachSize + ' bytes');
j := j + 1;
end;
WriteLn('--');
i := i + 1;
end;
// Disconnect from the IMAP server.
success := imap.Disconnect();
imap.Free;
messageSet.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.