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

Get the Content-Type of the Nth Matching Part

See more Email Object Examples

Demonstrates the Chilkat Email.GetNthContentType method, which returns the Content-Type of the Nth non-multipart MIME part matching a content-type pattern. The first argument is the zero-based index among the matching parts, the second is the pattern (exact or wildcard), the third is an inlineOnly flag, and the fourth is an excludeAttachments flag. This example reads the content type of the first text/* part.

Background: Where GetNthTextPartOfType returns a matching part's content, GetNthContentType returns its exact type. A wildcard pattern like text/* can match several parts (say text/plain and text/html), so pairing this with GetNumPartsOfType lets you enumerate the parts and learn precisely what each one is before fetching it.

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

begin
  //  Demonstrates the GetNthContentType method, which returns the Content-Type 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 pattern, the third is inlineOnly,
  //  and the fourth is excludeAttachments.

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

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

  //  Get the content type of the first (index 0) part matching text/*.
  ct := email.GetNthContentType(0,'text/*',False,False);
  WriteLn('First text/* part content type: ' + ct);


  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.