Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Add Duplicate Header Fields to an Email
See more Email Object Examples
Demonstrates the Chilkat Email.AddHeaderField2 method. It works like AddHeaderField, except that if the header field already exists it is not replaced — a duplicate header is added instead. Use it for headers that may legally occur more than once, such as Received. This example adds two headers with the same name and prints the header showing both.
Background: The email standards allow certain header fields to repeat. The classic case is
Received: each mail server that handles a message prepends its own Received line, so a delivered email typically has several, forming a trace of the path it took. Single-valued fields like Subject should be replaced (use AddHeaderField), whereas repeatable fields need AddHeaderField2 so earlier occurrences are preserved rather than overwritten.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
begin
// Demonstrates the AddHeaderField2 method. It is the same as AddHeaderField, except that
// if the header field already exists, it is NOT replaced -- a duplicate header is added.
// Use this for headers that may legally occur more than once, such as "Received".
email := TEmail.Create;
email.Subject := 'Duplicate header example';
email.From := 'alice@example.com';
// Add two header fields with the same name; both are retained.
email.AddHeaderField2('X-Trace','hop-1');
email.AddHeaderField2('X-Trace','hop-2');
// Both X-Trace fields are present in the header.
WriteLn(email.Header);
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.