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

Add a Related Item from BinData by Content-Location

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedBd2 method, which adds a related item using the binary data in a BinData object, addressed by Content-Location. The second argument specifies the filename, path, or URL used by the corresponding HTML reference. This example embeds an image referenced as logo.png.

Background: This is the binary counterpart of AddRelatedString2 and the Content-Location sibling of AddRelatedBd. Instead of a generated cid: reference, the HTML keeps an ordinary src="logo.png" and the related part is matched to it by name — convenient when turning an existing web page into an email so its original relative URLs keep working.

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;
  bdImage: TBinData;

begin
  success := False;

  //  Demonstrates the AddRelatedBd2 method, which adds a related item using the binary data in
  //  a BinData object, addressed by Content-Location.  The second argument specifies the filename/path/URL
  //  used by the corresponding HTML reference.

  email := TEmail.Create;
  email.Subject := 'Related image from BinData (Content-Location)';

  //  The HTML references the image by the same name used as the Content-Location.
  email.SetHtmlBody('<html><body><img src="logo.png"/></body></html>');

  //  Load the image data from a file into a BinData object.
  bdImage := TBinData.Create;
  success := bdImage.LoadFile('qa_data/images/logo.png');
  if (success = False) then
    begin
      WriteLn(bdImage.LastErrorText);
      Exit;
    end;

  //  Add the image as a related item addressed by Content-Location "logo.png".
  success := email.AddRelatedBd2(bdImage,'logo.png');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

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

  //  Note: The path "qa_data/images/logo.png" is a relative local filesystem path,
  //  relative to the current working directory of the running application.


  email.Free;
  bdImage.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.