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

Attach a File with an Explicit Content Type

See more Email Object Examples

Demonstrates the Chilkat Email.AddFileAttachment2 method, which attaches a file from the filesystem and lets you explicitly specify its content type rather than having Chilkat infer it from the file extension. This example attaches a binary file as application/octet-stream.

Background: Extension-based type detection is convenient but not always right — a file may have an unusual or missing extension, or you may need a very specific MIME type for the recipient to process it correctly. Specifying the content type explicitly removes the guesswork. application/octet-stream is the generic "arbitrary binary data" type, a safe default that tells the client to treat the attachment as an opaque download rather than trying to render it.

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.Email;

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;

begin
  success := False;

  //  Demonstrates the AddFileAttachment2 method, which attaches a file and lets you
  //  explicitly specify its content type instead of letting Chilkat infer it.

  email := TEmail.Create;
  email.Subject := 'Email with a file attachment';
  email.Body := 'Please see the attached file.';

  //  Attach a file, explicitly specifying the content type.
  success := email.AddFileAttachment2('qa_data/attachments/data.bin','application/octet-stream');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('NumAttachments = ' + email.NumAttachments);

  //  Note: The path "qa_data/attachments/data.bin" is a relative local filesystem path,
  //  relative to the current working directory of the running application.


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