Sample code for 30+ languages & platforms
Lazarus Pascal

Get a MIME Header Field Value

See more MIME Examples

Demonstrates the Chilkat Mime.GetHeaderField method, which returns the value of the first header field whose name matches case-insensitively. The only argument is the field name; it returns null when no matching field exists.

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: This is the everyday way to read a header value — the Subject, a Message-ID, a custom X- header. Because a missing field returns null, the returned value is assigned to a variable and checked rather than used directly, so an absent header is handled cleanly instead of surfacing as an error later. Only the first matching occurrence is returned; iterate by index to see duplicates.

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

begin
  success := False;

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

  //  Return the value of the first header field with a matching (case-insensitive) name.
  //  GetHeaderField returns null when no matching field exists, so assign it to a string variable
  //  and check for success.
  subject := mime.GetHeaderField('Subject');
  if (mime.LastMethodSuccess = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  WriteLn('Subject: ' + subject);


  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.