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

Iterate MIME Parts of an Email

See more Email Object Examples

Demonstrates how to iterate over the MIME sub-parts of an email, and retrieve the content of each MIME sub-part body.

Note: This example requires some new features added to Chilkat v9.5.0.95.

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.StringBuilder,
  Chilkat.BinData,
  Chilkat.Email;

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  sbContentType: TStringBuilder;
  caseSensitive: Boolean;
  inlineOnly: Boolean;
  excludeAttachments: Boolean;
  searchSpec: string;
  numParts: Integer;
  i: Integer;
  textBody: string;
  attachedEmail: TEmail;
  bdMime: TBinData;
  bd: TBinData;

begin
  success := False;

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

  //  See the following Chilkat post to Quickly Understand Email MIME

  email := TEmail.Create;

  success := email.LoadEml('qa_data/eml/sample.eml');
  if (success = False) then
    begin
      WriteLn('Failed to load .eml');
      Exit;
    end;

  sbContentType := TStringBuilder.Create;
  caseSensitive := False;

  //  Get the total number of non-multipart MIME sub-parts.
  //  (This is a simple way of iterating over all the MIME leaf parts regardless of the MIME tree structure)
  inlineOnly := False;
  excludeAttachments := False;
  searchSpec := '*/*';

  numParts := email.GetNumPartsOfType(searchSpec,inlineOnly,excludeAttachments);
  i := 0;
  while i < numParts do
    begin
      //  What is the Content-Type of this MIME part?
      sbContentType.Append(email.GetNthContentType(i,searchSpec,inlineOnly,excludeAttachments));
      if (sbContentType.StartsWith('text/',caseSensitive) = True) then
        begin
          //  Get the text body of this MIME part.
          textBody := email.GetNthTextPartOfType(i,searchSpec,inlineOnly,excludeAttachments);
          WriteLn('Got text body for ' + sbContentType.GetAsString());
        end
      else
        begin
          if (sbContentType.ContentsEqual('message/rfc822',caseSensitive) = True) then
            begin
              //  If the Content-Type is message/rfc822, then the MIME body for this part contains a full embedded MIME messages.
              //  Your application could load it into a Chilkat email object and recursively process...
              attachedEmail := TEmail.Create;
              bdMime := TBinData.Create;
              email.GetNthBinaryPartOfTypeBd(i,searchSpec,inlineOnly,excludeAttachments,bdMime);
              attachedEmail.SetFromMimeBd(bdMime);
              //  Now your app can recursively process the attachedEmail...
            end
          else
            begin
              //  Get the bytes of this MIME body part.
              bd := TBinData.Create;
              email.GetNthBinaryPartOfTypeBd(i,searchSpec,inlineOnly,excludeAttachments,bd);
              WriteLn('Got binary body for ' + sbContentType.GetAsString() + ' numBytes = ' + bd.NumBytes);
            end;
        end;
      sbContentType.Clear();
      i := i + 1;
    end;



  email.Free;
  sbContentType.Free;
              attachedEmail.Free;
              bdMime.Free;
              bd.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.