Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Read text and binary MIME parts by content type

See more Email Object Examples

Use the same wildcard and inclusion flags for both counting and retrieval. Request decoded text for textual parts and BinData for binary parts. Use GetAttachedEmail when the goal is to inspect a nested email as a message.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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;
  types: string;
  inlineOnly: Boolean;
  excludeAttachments: Boolean;
  count: Integer;
  mediaType: TStringBuilder;
  i: Integer;
  partText: string;
  data: TBinData;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  email := TEmail.Create;
  success := email.LoadEml('workshop.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  types := '*/*';
  inlineOnly := False;
  excludeAttachments := False;
  count := email.GetNumPartsOfType(types,inlineOnly,excludeAttachments);
  mediaType := TStringBuilder.Create;
  i := 0;
  while (i < count) do
    begin
      mediaType.SetString(email.GetNthContentType(i,types,inlineOnly,excludeAttachments));
      if (mediaType.StartsWith('text/',False) = True) then
        begin
          partText := email.GetNthTextPartOfType(i,types,inlineOnly,excludeAttachments);
          if (email.LastMethodSuccess <> True) then
            begin
              WriteLn(email.LastErrorText);
              Exit;
            end;
          WriteLn(mediaType.GetAsString() + '  ' + partText);
        end
      else
        begin
          data := TBinData.Create;
          success := email.GetNthBinaryPartOfTypeBd(i,types,inlineOnly,excludeAttachments,data);
          if (success <> True) then
            begin
              WriteLn(email.LastErrorText);
              Exit;
            end;
          WriteLn(mediaType.GetAsString() + '  ' + data.NumBytes + ' bytes');
        end;
      i := i + 1;
    end;



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