![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi ActiveX) Read IMAP Email HeadersThis 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:
This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage. Note: This example requires Chilkat v11.0.0 or greater.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB; ... procedure TForm1.Button1Click(Sender: TObject); var success: Integer; imap: TChilkatImap; fetchUids: Integer; messageSet: TMessageSet; bundle: TChilkatEmailBundle; headersOnly: Integer; email: TChilkatEmail; name: WideString; addr: WideString; i: Integer; j: Integer; numAttach: Integer; attachSize: Integer; begin success := 0; success := 0; // This example assumes the Chilkat API has already been unlocked. // See Global Unlock Sample for example code. imap := TChilkatImap.Create(Self); // Connect to the IMAP server using SSL/TLS on the standard secure IMAP port. imap.Ssl := 1; imap.Port := 993; success := imap.Connect('imap.example.com'); if (success = 0) then begin Memo1.Lines.Add(imap.LastErrorText); Exit; end; // Authenticate with the IMAP server. success := imap.Login('****','****'); if (success = 0) then begin Memo1.Lines.Add(imap.LastErrorText); Exit; end; // Select the mailbox (folder) to access. success := imap.SelectMailbox('Inbox'); if (success = 0) then begin Memo1.Lines.Add(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 := 1; messageSet := TMessageSet.Create(Self); success := imap.QueryMbx('ALL',fetchUids,messageSet.ControlInterface); if (success = 0) then begin Memo1.Lines.Add(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 := TChilkatEmailBundle.Create(Self); headersOnly := 1; success := imap.FetchMsgSet(headersOnly,messageSet.ControlInterface,bundle.ControlInterface); if (success = 0) then begin Memo1.Lines.Add(imap.LastErrorText); Exit; end; // Iterate over each email in the bundle and display summary information. email := TChilkatEmail.Create(Self); i := 0; j := 0; while i < bundle.MessageCount do begin bundle.EmailAt(i,email.ControlInterface); // Display the sender and subject line. Memo1.Lines.Add(email.From); Memo1.Lines.Add(email.Subject); // Display all "To" recipients. j := 0; while j < email.NumTo do begin Memo1.Lines.Add('TO: ' + email.GetToName(j) + ', ' + email.GetToAddr(j)); j := j + 1; end; // Display all "CC" recipients. j := 0; while j < email.NumCC do begin Memo1.Lines.Add('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. Memo1.Lines.Add(IntToStr(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.ControlInterface); j := 0; while j < numAttach do begin // Display the attachment filename. Memo1.Lines.Add(imap.GetMailAttachFilename(email.ControlInterface,j)); // Display the attachment size in bytes. attachSize := imap.GetMailAttachSize(email.ControlInterface,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 := imap.Disconnect(); end; |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.