Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Add a Related Item from a BinData Object
See more Email Object Examples
Demonstrates the Chilkat Email.AddRelatedBd method, which adds a related item (such as an inline image) using the contents of a BinData object, and returns the generated Content-ID. This example loads an image into a BinData, adds it, captures the Content-ID, and references it from the HTML body.
Background: This is the binary,
Content-ID-based way to embed an inline resource — the right choice for images, whose bytes belong in a BinData rather than a string. Because Chilkat generates the Content-ID, the usual pattern is: add the item first, capture the returned ID, then build the matching <img src="cid:..."> reference from it (done here with a StringBuilder).Chilkat Pascal (Lazarus/Delphi) Downloads
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;
bdImage: TBinData;
cid: string;
sbHtml: TStringBuilder;
numReplaced: Integer;
begin
success := False;
// Demonstrates the AddRelatedBd method, which adds a related item (such as an inline image)
// using the contents of a BinData object, and returns the generated Content-ID.
email := TEmail.Create;
email.Subject := 'Related image from BinData';
// 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; capture its generated Content-ID.
cid := email.AddRelatedBd('logo.png',bdImage);
if (email.LastMethodSuccess = False) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
// Reference the related item in the HTML body by its Content-ID.
sbHtml := TStringBuilder.Create;
sbHtml.Append('<html><body><img src="cid:PLACEHOLDER_CID"/></body></html>');
numReplaced := sbHtml.Replace('PLACEHOLDER_CID',cid);
email.SetHtmlBody(sbHtml.GetAsString());
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;
sbHtml.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.