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

Read Delivery-Status Fields from a Bounce

See more Email Object Examples

Demonstrates the Chilkat Email.GetDeliveryStatusInfo method, which returns a named field from the message/delivery-status part of a delivery-status notification (DSN). It should be called only for an email identified as a delivery-status notification. This example loads a bounce message and reads its Action, Status, and Final-Recipient fields.

Background: When a message can't be delivered, the mail system returns a bounce as a multipart/report whose message/delivery-status part carries machine-readable fields: Action (e.g. failed), Status (a numeric code like 5.1.1 meaning "no such mailbox"), and Final-Recipient (the address that failed). Parsing these lets a program automatically detect hard bounces and prune bad addresses from a mailing list.

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
  success: Boolean;
  email: TEmail;

begin
  success := False;

  //  Demonstrates the GetDeliveryStatusInfo method, which returns a field from the
  //  message/delivery-status part of a delivery-status notification (DSN / bounce).
  //  Call it only for an email that is a delivery-status notification.

  email := TEmail.Create;

  success := email.LoadEml('qa_data/eml/dsn_bounce.eml');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  Read individual fields from the message/delivery-status part.
  WriteLn('Action: ' + email.GetDeliveryStatusInfo('Action'));
  WriteLn('Status: ' + email.GetDeliveryStatusInfo('Status'));
  WriteLn('Final-Recipient: ' + email.GetDeliveryStatusInfo('Final-Recipient'));

  //  Note: The path "qa_data/..." is a relative local filesystem path,
  //  relative to the current working directory of the running application.


  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.