Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Count the Messages in a multipart/digest Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumDigests property, which is the number of message/rfc822 parts contained within a multipart/digest enclosure. If no such enclosure exists, the value is 0. Digest indexes are zero-based; use GetDigestEmail to retrieve an individual bundled message as its own Email object. This example loads a digest email and lists each message's subject.

Background: A multipart/digest is a way to package many separate emails inside one carrier message — historically used by mailing lists to send a single daily "digest" containing all of that day's posts. Each bundled item is a complete message/rfc822 email with its own headers and body. This differs from NumAttachedMessages, which counts nested messages attached to an ordinary email rather than the members of a digest enclosure.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Email;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  n: Integer;
  digestEmail: TEmail;
  i: Integer;

begin
  success := False;

  //  Demonstrates the read-only Email.NumDigests property, which is the number of
  //  message/rfc822 parts contained within a multipart/digest enclosure.  If there is no
  //  multipart/digest enclosure, the value is 0.  Digest indexes are zero-based.

  email := TEmail.Create;

  //  Load a multipart/digest email (a single email that bundles many messages).
  success := email.LoadEml('qa_data/eml/digest.eml');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  n := email.NumDigests;
  WriteLn('NumDigests = ' + n);

  //  Retrieve each bundled message as its own Email object.
  digestEmail := TEmail.Create;

  for i := 0 to n - 1 do
    begin
      success := email.GetDigestEmail(i,digestEmail);
      WriteLn('Digest ' + i + ' subject: ' + digestEmail.Subject);
    end;

  //  Note: Paths such as "qa_data/..." are relative local filesystem paths,
  //  relative to the current working directory of the running application.


  email.Free;
  digestEmail.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.