Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, EmailBundle, Imap, Email, MessageSet;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
imap: HCkImap;
fetchUids: Boolean;
messageSet: HCkMessageSet;
bundle: HCkEmailBundle;
headersOnly: Boolean;
email: HCkEmail;
name: PWideChar;
addr: PWideChar;
i: Integer;
j: Integer;
numAttach: Integer;
attachSize: Integer;
begin
success := False;
success := False;
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for example code.
imap := CkImap_Create();
// Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);
success := CkImap_Connect(imap,'imap.example.com');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Authenticate with the IMAP server.
success := CkImap_Login(imap,'****','****');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Select the mailbox (folder) to access.
success := CkImap_SelectMailbox(imap,'Inbox');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
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 := CkMessageSet_Create();
success := CkImap_QueryMbx(imap,'ALL',fetchUids,messageSet);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
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 := CkEmailBundle_Create();
headersOnly := True;
success := CkImap_FetchMsgSet(imap,headersOnly,messageSet,bundle);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Iterate over each email in the bundle and display summary information.
email := CkEmail_Create();
i := 0;
j := 0;
while i < CkEmailBundle_getMessageCount(bundle) do
begin
CkEmailBundle_EmailAt(bundle,i,email);
// Display the sender and subject line.
Memo1.Lines.Add(CkEmail__from(email));
Memo1.Lines.Add(CkEmail__subject(email));
// Display all "To" recipients.
j := 0;
while j < CkEmail_getNumTo(email) do
begin
Memo1.Lines.Add('TO: ' + CkEmail__getToName(email,j) + ', ' + CkEmail__getToAddr(email,j));
j := j + 1;
end;
// Display all "CC" recipients.
j := 0;
while j < CkEmail_getNumCC(email) do
begin
Memo1.Lines.Add('CC: ' + CkEmail__getCcName(email,j) + ', ' + CkEmail__getCcAddr(email,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.
Memo1.Lines.Add(IntToStr(CkEmail_getSize(email)));
// 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 := CkImap_GetMailNumAttach(imap,email);
j := 0;
while j < numAttach do
begin
// Display the attachment filename.
Memo1.Lines.Add(CkImap__getMailAttachFilename(imap,email,j));
// Display the attachment size in bytes.
attachSize := CkImap_GetMailAttachSize(imap,email,j);
Memo1.Lines.Add(' size = ' + IntToStr(attachSize) + ' bytes');
j := j + 1;
end;
Memo1.Lines.Add('--');
i := i + 1;
end;
// Disconnect from the IMAP server.
success := CkImap_Disconnect(imap);
CkImap_Dispose(imap);
CkMessageSet_Dispose(messageSet);
CkEmailBundle_Dispose(bundle);
CkEmail_Dispose(email);
end;