Sample code for 30+ languages & platforms
Lazarus Pascal

Add a MIME Header Field

See more MIME Examples

Demonstrates the Chilkat Mime.AddHeaderField method, which appends a new header field. The first argument is the field name and the second is the value. Existing fields with the same case-insensitive name are preserved, so this can create duplicates.

Background: MIME headers are ordered name/value lines, and some — Received, for instance — legitimately appear more than once. AddHeaderField is the tool for that: it always appends, never replacing an existing field of the same name. When you instead want exactly one field with a given name, use SetHeaderField, which replaces rather than accumulates.

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

begin
  success := False;

  //  Demonstrates the Mime.AddHeaderField method, which appends a new header field.  The 1st argument
  //  is the field name and the 2nd is the value.  Existing fields with the same (case-insensitive)
  //  name are preserved, so this can create duplicate fields.

  mime := TMime.Create;

  success := mime.SetBodyFromPlainText('Hello.');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Append header fields.  AddHeaderField does not replace existing fields of the same name.
  success := mime.AddHeaderField('X-Custom-Header','value1');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  success := mime.AddHeaderField('X-Priority','3');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  headers := mime.GetEntireHead();
  if (mime.LastMethodSuccess = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  WriteLn(headers);


  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.