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

Set a MIME Body from a File

See more MIME Examples

Demonstrates the Chilkat Mime.SetBodyFromFile method, which reads a local file and sets it as the body, choosing content headers from the filename extension. The only argument is the file path.

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 one-call way to turn a file into a MIME part: Chilkat reads the bytes and infers the Content-Type and transfer encoding from the extension — .jpg becomes image/jpeg with base64, .txt a text type, and so on. It is the natural building block for adding an attachment, usually followed by setting a Content-Disposition and filename.

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;

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

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

begin
  success := False;

  //  Demonstrates the Mime.SetBodyFromFile method, which reads a local file and sets it as the body,
  //  choosing content headers based on the filename extension.  The only argument is the file path.

  mime := TMime.Create;

  //  The Content-Type and encoding are set from the ".jpg" extension (image/jpeg, base64).
  success := mime.SetBodyFromFile('qa_data/photo.jpg');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn('Content-Type: ' + mime.ContentType);
  WriteLn('Encoding: ' + mime.Encoding);


  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.