Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get an Alternative Body by Content-Type
See more Email Object Examples
Demonstrates the Chilkat Email.GetAlternativeBodyByContentType method, which returns an alternative body selected by its Content-Type — such as text/plain, text/html, or text/xml — instead of by numeric index. This example retrieves both the plain-text and HTML alternatives by content type.
Background: Selecting by
Content-Type is often more convenient than by index, because you usually know which representation you want (the HTML, say) but not its position in the alternative list. This mirrors how a mail client works — it looks up the best matching type to display — and avoids assumptions about ordering, which can vary between messages.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;
plain: string;
htmlBody: string;
begin
// Demonstrates the GetAlternativeBodyByContentType method, which returns an alternative
// body selected by its Content-Type (such as text/plain, text/html, or text/xml) rather
// than by index.
email := TEmail.Create;
// Create an email with plain-text and HTML alternatives.
email.SetTextBody('This is the plain-text alternative.','text/plain');
email.AddHtmlAlternativeBody('<html><body>This is the HTML alternative.</body></html>');
// Get the plain-text alternative by its content type.
plain := email.GetAlternativeBodyByContentType('text/plain');
WriteLn('text/plain body: ' + plain);
// Get the HTML alternative by its content type.
htmlBody := email.GetAlternativeBodyByContentType('text/html');
WriteLn('text/html body: ' + htmlBody);
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.