Sample code for 30+ languages & platforms
Lazarus Pascal

Enumerate MIME Header Field Names

See more MIME Examples

Demonstrates the Chilkat Mime.GetHeaderFieldName method, which returns the name of the header-field occurrence at a zero-based index. The only argument is the index; use NumHeaderFields for the count. Original order and casing are preserved.

Note: The file path is relative to the application's current working directory. Absolute paths may also be used. Supply the path to your own file.

Background: When you need to walk all the headers — to display them, copy them, or handle duplicates that GetHeaderField would hide — index-based access is the way. NumHeaderFields bounds the loop, and this returns each field's name in its original order and casing, which matters when the exact header sequence is significant.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.Mime;

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  n: Integer;
  i: Integer;
  fieldName: string;

begin
  success := False;

  mime := TMime.Create;
  success := mime.LoadMimeFile('qa_data/message.eml');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Iterate the header fields by index.  NumHeaderFields gives the count; GetHeaderFieldName and
  //  GetHeaderFieldValue return the name and value at each zero-based index.  Original order and
  //  casing are preserved.
  n := mime.NumHeaderFields;
  WriteLn('Header fields: ' + n);

  for i := 0 to n - 1 do
    begin
      fieldName := mime.GetHeaderFieldName(i);
      if (mime.LastMethodSuccess = False) then
        begin
          WriteLn(mime.LastErrorText);
          Exit;
        end;
      WriteLn(fieldName);
    end;



  mime.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.