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

Remove a Header Field from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveHeaderField method, which removes by name all occurrences of a header field. Header-field names are case-insensitive, and every repeated occurrence with the given name is removed. This example adds a custom header, then removes it.

Background: Editing a message sometimes means deleting a header outright — stripping a tracking header, removing an X- flag before forwarding, or clearing a stale field before re-sending. Because a field name can appear more than once (like Received), RemoveHeaderField removes all matching occurrences in one call rather than just the first.

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;
  hasBefore: Boolean;
  hasAfter: Boolean;

begin
  //  Demonstrates the RemoveHeaderField method, which removes by name all occurrences of a
  //  header field.  Header-field names are case-insensitive.

  email := TEmail.Create;
  email.Subject := 'Remove header field';
  email.From := 'alice@example.com';

  email.AddHeaderField('X-Custom-Header','some value');

  //  Use HasHeaderMatching to test for the header's presence.  ("*" matches any value.)
  hasBefore := email.HasHeaderMatching('X-Custom-Header','*',False);
  WriteLn('Has X-Custom-Header before: ' + hasBefore);

  //  Remove all occurrences of the header field.
  email.RemoveHeaderField('X-Custom-Header');

  hasAfter := email.HasHeaderMatching('X-Custom-Header','*',False);
  WriteLn('Has X-Custom-Header after: ' + hasAfter);


  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.