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

Add an Attachment from a BinData Object

See more Email Object Examples

Demonstrates the Chilkat Email.AddAttachmentBd method, which adds an attachment using the contents of a BinData object. The first argument is the attachment filename, the second is the BinData, and the third is the content type — if empty, it is inferred from the filename extension. This example loads a PDF into a BinData and attaches it.

Background: BinData is Chilkat's container for raw binary data. Attaching from a BinData is the right approach when the file's bytes are already in memory — generated on the fly, downloaded, or read from a database — rather than sitting on disk (which would use AddFileAttachment). Chilkat Base64-encodes the bytes into the message automatically.

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

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

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

begin
  success := False;

  //  Demonstrates the AddAttachmentBd method, which adds an attachment using the contents of a
  //  BinData object.  The first argument is the attachment filename, the second is the BinData
  //  object, and the third is the content type (inferred from the filename extension if empty).

  email := TEmail.Create;
  email.Subject := 'Attach from BinData';
  email.Body := 'Please see the attached file.';

  //  Load a file into a BinData object, then attach it.
  bd := TBinData.Create;
  success := bd.LoadFile('qa_data/attachments/report.pdf');
  if (success = False) then
    begin
      WriteLn(bd.LastErrorText);
      Exit;
    end;

  success := email.AddAttachmentBd('report.pdf',bd,'application/pdf');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

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

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


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