Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Add a Related String Addressed by Name
See more Email Object Examples
Demonstrates the Chilkat Email.AddRelatedString2 method, which adds a related item from an in-memory string, addressed by the name used in the HTML rather than by a generated Content-ID. The first argument should match the filename used in an HTML img src or a stylesheet link href; the second is the Unicode content; the third is the charset it is converted to. Related items are images and style sheets embedded to support the HTML display — they are not attachments. This example embeds a CSS style sheet referenced as styles.css.
Background: This is the string-based,
Content-Location counterpart to AddRelatedString. Instead of the HTML pointing at a cid: URL, it keeps a natural reference like href="styles.css", and the related part is matched to it by name. That makes it a good fit when you are turning existing web content into email and want the original relative references to keep working without rewriting them.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.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
email: TEmail;
begin
// Demonstrates the AddRelatedString2 method, which adds a related item to the email from
// an in-memory string, addressed by the name used in the HTML (Content-Location). The first
// argument is the filename used in the HTML reference, the second is the content, and the
// third is the charset.
email := TEmail.Create;
email.Subject := 'Email with a related style sheet (by name)';
// The HTML references the style sheet by the same name used for the related item.
email.SetHtmlBody('<html><head><link rel="stylesheet" href="styles.css"/></head><body>Styled.</body></html>');
// Add the style sheet as a related item addressed by the name used in the HTML.
email.AddRelatedString2('styles.css','body { color: navy; }','utf-8');
WriteLn('NumRelatedItems = ' + email.NumRelatedItems);
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.