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

Add a Custom Header to an Email Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.AddAttachmentHeader method, which adds a custom MIME header field to an existing attachment identified by its zero-based index. This example adds an attachment (which becomes index 0), then attaches an extra header field to it and prints the resulting MIME.

Background: In MIME, every attachment is itself a mini-message with its own small block of headers — things like Content-Type, Content-Disposition, and Content-Transfer-Encoding that describe that specific part. AddAttachmentHeader lets you insert additional fields into that per-part header block, which is occasionally needed for interoperability with systems that look for custom X- headers or content identifiers on individual attachments.

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;

begin
  //  Demonstrates the AddAttachmentHeader method, which adds a custom MIME header field to
  //  an existing attachment, identified by its zero-based index.

  email := TEmail.Create;
  email.Subject := 'Attachment with a custom header';
  email.Body := 'The attachment has an extra MIME header.';

  //  Add an attachment; it becomes attachment index 0.
  email.AddStringAttachment('data.txt','Attachment body.');

  //  Add a custom header field to the first attachment (index 0).
  email.AddAttachmentHeader(0,'X-Custom-Attachment-Header','some value');

  //  The custom header now appears in the attachment's MIME part.
  WriteLn(email.GetMime());


  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.