Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Add an attachment and an inline image

See more Email Object Examples

Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.BinData,
  Chilkat.StringBuilder,
  Chilkat.Email;

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  data: TBinData;
  image: TBinData;
  cid: string;
  html: TStringBuilder;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  email := TEmail.Create;
  success := email.LoadEml('text-and-html.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  data := TBinData.Create;
  success := data.AppendString('Time,Activity' + #13#10 + '09:00,Welcome' + #13#10 + '10:00,Field session' + #13#10,'utf-8');
  if (success <> True) then
    begin
      WriteLn('Failed: data.AppendString.');
      Exit;
    end;
  success := email.AddAttachmentBd('agenda.csv',data,'text/csv');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
  image := TBinData.Create;
  success := image.AppendEncoded('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==','base64');
  if (success <> True) then
    begin
      WriteLn('Failed: image.AppendEncoded.');
      Exit;
    end;
  cid := email.AddRelatedBd('confirmed.png',image);
  if (email.LastMethodSuccess <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  html := TStringBuilder.Create;
  html.Append('<p><img alt=''Confirmed'' src="cid:');
  html.Append(cid);
  html.Append('"></p>');
  html.Append(email.GetHtmlBody());
  success := email.AddHtmlAlternativeBody(html.GetAsString());
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  success := email.SaveEml('with-attachments.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  WriteLn('Saved with-attachments.eml.');


  email.Free;
  data.Free;
  image.Free;
  html.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.