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

Get the Decoded MIME Body Bytes (BinData)

See more MIME Examples

Demonstrates the Chilkat Mime.GetBodyBd method, which replaces the contents of a BinData with the decoded body bytes of the current part. The only argument is the BinData. Base64 and quoted-printable are decoded; headers and boundaries are not included.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is how you extract a part's actual content as bytes — the decoded payload of an attachment or an image, ready to hash, re-save, or hand to another API. Because it works in raw bytes it is the correct choice for binary parts, where a text decode would be wrong. Note it replaces the BinData contents rather than appending.

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

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  bd: TBinData;

begin
  success := False;

  mime := TMime.Create;
  success := mime.LoadMimeFile('qa_data/message.eml');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Replace the contents of a BinData with the decoded body bytes of the current part.  Base64 and
  //  quoted-printable are decoded; MIME headers and multipart boundaries are not included.
  bd := TBinData.Create;
  success := mime.GetBodyBd(bd);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  WriteLn('Decoded body: ' + bd.NumBytes + ' bytes.');


  mime.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.