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

Load .eml and Examine the Structure, Attachments, and Related Items

See more Email Object Examples

Demonstrates how to load examine the MIME structure of a .eml, and also examine the attachment and related item filenames, attached messages, and multipart/report and DSN information.

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.Mime,
  Chilkat.Email,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  emlPath: string;
  mime: TMime;
  email: TEmail;
  i: Integer;
  numAttach: Integer;
  numRelated: Integer;
  em: TEmail;
  numAttachedMessages: Integer;
  numReports: Integer;
  jsonDsnInfo: TJsonObject;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  emlPath := 'C:/AAWorkarea/beatrix/roesner.eml';

  mime := TMime.Create;

  success := mime.LoadMimeFile(emlPath);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn('---- MIME structure ----');
  WriteLn(mime.GetStructure('text'));
  WriteLn('------------------------');

  email := TEmail.Create;
  success := email.LoadEml(emlPath);

  //  Was this a signed and/or encrypted email?
  //  If so, then loading the .eml automatically unwraps
  //  (i.e. verifies signatures and decrypts) and the resultant
  //  email is what existed prior to signing/encrypting.
  WriteLn('Email was Signed: ' + email.ReceivedSigned);
  WriteLn('Email was Encrypted: ' + email.ReceivedEncrypted);
  if (email.ReceivedSigned = True) then
    begin
      WriteLn('Signature(s) valid = ' + email.SignaturesValid);
    end;
  if (email.ReceivedEncrypted = True) then
    begin
      WriteLn('Decrypted successfully = ' + email.Decrypted);
    end;

  i := 0;
  numAttach := email.NumAttachments;
  WriteLn('Number of attachments = ' + numAttach);

  while i < numAttach do
    begin
      WriteLn('---- Attachment ' + i);

      //  Examine the filename (if any)
      WriteLn('filename: ' + email.GetAttachmentFilename(i));
      //  Examine the content-ID (if any)
      WriteLn('Content-ID: ' + email.GetAttachmentContentID(i));
      //  Examine the content-type
      WriteLn('Content-Type: ' + email.GetAttachmentContentType(i));
      //  Examine the content-disposition
      WriteLn('Content-Disposition' + email.GetAttachmentHeader(i,'content-disposition'));
      //  Examine the attachment size:
      WriteLn('Size (in bytes) of the attachment: ' + email.GetAttachmentSize(i));

      i := i + 1;
    end;

  WriteLn('--');

  //  Now for the related items.

  //  Note: A MIME sub-part can potentially be both a related item AND an attachment.
  //  The typical case is when the item is contained under the multipart/related enclosure and 
  //  the item also has a "Content-Disposition" header indicating "attachment".
  //  The location within multipart/related makes it a "related item", yet the Content-Disposition can also make it semantically an attachment.
  //  Related items and attachments are not necessarily mutually exclusive.

  numRelated := email.NumRelatedItems;
  WriteLn('Number of related items = ' + numRelated);
  i := 0;
  while i < numRelated do
    begin
      WriteLn('---- Related Item ' + i);

      //  Examine the filename (if any)
      WriteLn('filename: ' + email.GetRelatedFilename(i));
      //  Examine the content-ID (if any)
      WriteLn('Content-ID: ' + email.GetRelatedContentID(i));
      //  Examine the content-type
      WriteLn('Content-Type: ' + email.GetRelatedContentType(i));
      //  Examine the content-location (if any)
      WriteLn('Content-Location' + email.GetRelatedContentLocation(i));

      i := i + 1;
    end;

  //  The email could also have attached messages.
  //  An attached message is another email that was attached to this email.
  em := TEmail.Create;
  numAttachedMessages := email.NumAttachedMessages;
  WriteLn('Number of attached messages = ' + numAttachedMessages);
  i := 0;
  while i < numAttachedMessages do
    begin
      WriteLn('---- Attached message ' + i);

      //  Examine the attached email
      email.GetAttachedEmail(i,em);
      WriteLn('from: ' + em.From);
      WriteLn('subject: ' + em.Subject);
      i := i + 1;
    end;

  //  An email could also be a multipart/report email. 
  //  This is a DSN (Delivery Status Notification)
  //  The NumReports property indicates how many "reports" exist.
  numReports := email.NumReports;
  WriteLn('Number of reports = ' + numReports);
  i := 0;
  while i < numReports do
    begin
      WriteLn('---- Report ' + i);
      //  Get the raw report data...
      WriteLn(email.GetReport(i));
      i := i + 1;
    end;

  //  If the email is a multipart/report, then the information
  //  from the message/delivery-status part of the email can be retrieved:
  if (email.IsMultipartReport() = True) then
    begin

      WriteLn('--- Delivery Status Information:');
      WriteLn('Status: ' + email.GetDeliveryStatusInfo('Status'));
      WriteLn('Action: ' + email.GetDeliveryStatusInfo('Action'));
      WriteLn('Reporting-MTA: ' + email.GetDeliveryStatusInfo('Reporting-MTA'));

      jsonDsnInfo := TJsonObject.Create;
      email.GetDsnInfo(jsonDsnInfo);
      jsonDsnInfo.EmitCompact := False;
      WriteLn(jsonDsnInfo.Emit());
    end;


  mime.Free;
  email.Free;
  em.Free;
      jsonDsnInfo.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.