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

Remove the HTML Body from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveHtmlAlternative method, which removes the HTML body from the email if one exists. Other body representations, attachments, and related items remain unchanged. This example builds a message with both plain-text and HTML alternatives, then removes the HTML.

Background: A multipart/alternative message carries the same content as both HTML and plain text. Dropping the HTML alternative yields a leaner, text-only message — useful for reducing size, sidestepping HTML-rendering or spam concerns, or enforcing a plain-text policy. The plain-text alternative remains as the message body. Its counterpart is RemovePlainTextAlternative.

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;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  email: TEmail;

begin
  //  Demonstrates the RemoveHtmlAlternative method, which removes the HTML body from the email
  //  (if one exists).  Other body representations, attachments, and related items remain.

  email := TEmail.Create;
  email.Subject := 'Remove HTML alternative';

  //  Create an email with both plain-text and HTML alternatives.
  email.SetTextBody('This is the plain-text version.','text/plain');
  email.AddHtmlAlternativeBody('<html><body>This is the HTML version.</body></html>');
  WriteLn('NumAlternatives before = ' + email.NumAlternatives);
  WriteLn('HasHtmlBody before: ' + email.HasHtmlBody());

  //  Remove the HTML body, leaving the plain-text alternative.
  email.RemoveHtmlAlternative();
  WriteLn('NumAlternatives after = ' + email.NumAlternatives);
  WriteLn('HasHtmlBody after: ' + email.HasHtmlBody());


  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.