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

Add File Attachments to an Email

Demonstrates how to add one or more file attachments to an email.

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;
  contentType: string;

begin
  success := False;

  email := TEmail.Create;

  email.Subject := 'This is a test';
  email.Body := 'This is a test';
  email.From := 'support@chilkatsoft.com';
  success := email.AddTo('Chilkat Admin','admin@chilkatsoft.com');

  //  To add file attachments to an email, call AddFileAttachment
  //  once for each file to be attached.  The method returns
  //  the content-type of the attachment if successful, otherwise
  //  returns cknull

  contentType := email.AddFileAttachment('something.pdf');
  if (email.LastMethodSuccess <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  contentType := email.AddFileAttachment('something.xml');
  if (email.LastMethodSuccess <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  contentType := email.AddFileAttachment('something.zip');
  if (email.LastMethodSuccess <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  success := email.SaveEml('email.eml');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('Saved EML!');


  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.