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

Add a String Attachment to an Email

See more Email Object Examples

Demonstrates the Chilkat Email object's AddStringAttachment method, which adds an attachment directly from an in-memory string — no file needs to exist on disk. The 1st argument is the filename that is written into the MIME (it is not a path to a file that is read), and the 2nd argument is the text that becomes the attachment's body. In this example we build a simple email, attach a small CSV file named people.csv straight from a string, and then print the resulting MIME so you can see the attachment embedded in the message.

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
  email: TEmail;
  csvData: string;

begin
  //  This example demonstrates the Email object's AddStringAttachment method.
  //  AddStringAttachment adds an attachment directly from an in-memory string.
  //  The 1st argument is the filename to be placed in the MIME (not a file to be read).
  //  The 2nd argument is the text content that becomes the attachment body.

  email := TEmail.Create;

  email.Subject := 'Email with a string attachment';
  email.Body := 'See the attached CSV file.';
  email.From := 'mary@example.com';
  email.AddTo('Joe','joe@example.com');

  //  The text content of the attachment.
  csvData := 'id,name' + #13#10 + '1,Alice' + #13#10 + '2,Bob' + #13#10;

  //  Add the string as an attachment named "people.csv".
  email.AddStringAttachment('people.csv',csvData);

  //  Show the full MIME of the email, which now includes the attachment.
  WriteLn(email.GetMime());


  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.