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

Get the Name of the Nth Header Field

See more Email Object Examples

Demonstrates the Chilkat Email.GetHeaderFieldName method, which returns the name of the Nth header field. The NumHeaderFields property gives the count, and indexing is zero-based. This example enumerates the names of all header fields.

Background: Enumerating headers by index — rather than looking them up by name — lets you discover which fields a message actually contains and see repeated fields (like multiple Received lines) in order. Pair GetHeaderFieldName with GetHeaderFieldValue at the same index to walk the entire header block as name/value pairs.

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;
  n: Integer;
  i: Integer;

begin
  //  Demonstrates the GetHeaderFieldName method, which returns the name of the Nth header
  //  field.  The NumHeaderFields property gives the number of header fields; indexing is
  //  zero-based.

  email := TEmail.Create;
  email.Subject := 'Enumerate header names';
  email.From := 'alice@example.com';
  email.AddTo('Bob','bob@example.com');

  n := email.NumHeaderFields;

  for i := 0 to n - 1 do
    begin
      WriteLn('Header ' + i + ' name: ' + email.GetHeaderFieldName(i));
    end;



  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.