Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get the Number of Body Alternatives in an Email
See more Email Object Examples
Demonstrates the read-only Chilkat Email.NumAlternatives property, which is the number of body representations present in the email. A representation may be plain text, HTML, iCalendar, or another body format. A normal email with one body returns 1, while a multipart/alternative email returns the number of alternatives it contains. Alternative indexes used by methods such as GetAlternativeBody are zero-based. This example builds an email with plain-text and HTML alternatives and prints the count.
Background: MIME organizes a message as a tree of parts.
multipart/alternative holds several versions of the same content (the client shows the best one it supports); multipart/mixed combines a body with attachments; and multipart/related bundles an HTML body with the inline images it references. A rich email nests these, for example:multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/png
application/pdfHere the text/plain part and the multipart/related part are the two alternatives.
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 read-only Email.NumAlternatives property, which is the
// number of body representations (plain text, HTML, iCalendar, etc.) in the email.
email := TEmail.Create;
// Set a plain-text body and add an HTML alternative. This creates a
// multipart/alternative email with two representations.
email.SetTextBody('This is the plain-text alternative.','text/plain');
email.AddHtmlAlternativeBody('<html><body><b>This is the HTML alternative.</b></body></html>');
// Alternative indexes (used by GetAlternativeBody) are zero-based.
WriteLn('NumAlternatives = ' + email.NumAlternatives);
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.