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

Get the Nth Text Part of a Given Content-Type

See more Email Object Examples

Demonstrates the Chilkat Email.GetNthTextPartOfType method, which returns the text content of the Nth non-multipart MIME part matching a Content-Type pattern. The arguments are a zero-based index among the matching parts, the content-type pattern, an inlineOnly flag, and an excludeAttachments flag. This example retrieves the first text/html part.

Background: A MIME message is a tree of parts, and sometimes you want a specific one by its type rather than by walking alternatives or bodies. The content-type pattern can be exact (text/html) or a wildcard (text/*), and the two boolean flags narrow the search — inlineOnly restricts to inline (displayed) parts, and excludeAttachments skips parts marked as attachments — so you can target exactly the text you care about.

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;
  htmlPart: string;

begin
  //  Demonstrates the GetNthTextPartOfType method, which returns the text content of the Nth
  //  non-multipart MIME part matching a Content-Type pattern.  The first argument is the zero-based
  //  index among matching parts, the second is the Content-Type pattern, the third is inlineOnly, and the fourth is
  //  excludeAttachments.

  email := TEmail.Create;
  email.Subject := 'GetNthTextPartOfType example';

  email.SetTextBody('This is the plain-text version.','text/plain');
  email.AddHtmlAlternativeBody('<html><body>This is the HTML version.</body></html>');

  //  Get the first (index 0) part whose content type matches text/html.
  htmlPart := email.GetNthTextPartOfType(0,'text/html',False,False);
  WriteLn('First text/html part:');
  WriteLn(htmlPart);


  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.