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

Enumerate the Header Fields of an Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumHeaderFields property together with GetHeaderFieldName and GetHeaderFieldValue to enumerate every header field. Indexing is zero-based, so fields run from 0 to NumHeaderFields - 1. Repeated header fields (a field name that appears more than once) are counted separately. This example builds a small email and prints each header field.

Background: A header field is a single Name: value line at the top of a MIME message. The same field name can legitimately appear multiple times — for example, a message can carry several Received lines, one added by each mail server it passed through. That is why enumerating by index (rather than looking up by name) matters: it lets you see every occurrence in the order it appears.

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 read-only Email.NumHeaderFields property and enumerating each
  //  header field by zero-based index using GetHeaderFieldName and GetHeaderFieldValue.

  email := TEmail.Create;
  email.Subject := 'Header field enumeration';
  email.From := 'mary@example.com';
  email.AddTo('Joe','joe@example.com');

  n := email.NumHeaderFields;
  WriteLn('NumHeaderFields = ' + n);

  for i := 0 to n - 1 do
    begin
      WriteLn(email.GetHeaderFieldName(i) + ': ' + email.GetHeaderFieldValue(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.