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

Set a MIME Body from Binary Bytes (BinData)

See more MIME Examples

Demonstrates the Chilkat Mime.SetBodyBd method, which sets the decoded body bytes from a BinData, changes the transfer encoding to base64, and preserves the bytes exactly. The only argument is the BinData.

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 the direct way to put arbitrary binary content into a part from memory — a generated image, a decrypted blob — without a temporary file. Chilkat base64-encodes it so the bytes survive text-based transports unchanged. Note it does not set a Content-Type or disposition, so assign those yourself to describe what the bytes are.

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;
  bd: TBinData;
  mime: TMime;

begin
  success := False;

  //  Demonstrates the Mime.SetBodyBd method, which sets the decoded body bytes from a BinData,
  //  changes the transfer encoding to base64, and preserves the bytes exactly.  The only argument is
  //  the BinData.

  //  Put the binary body bytes into a BinData.
  bd := TBinData.Create;
  success := bd.LoadFile('qa_data/photo.jpg');
  if (success = False) then
    begin
      WriteLn(bd.LastErrorText);
      Exit;
    end;

  mime := TMime.Create;

  //  Set a Content-Type for the binary content (SetBodyBd does not set one itself).
  mime.ContentType := 'image/jpeg';

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

  WriteLn('Body set from ' + bd.NumBytes + ' bytes.');


  bd.Free;
  mime.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.