Parse Non-Delivery Report (NDR) in Delphi

In previous section, I introduced how to convert email to HTML page. In this section, I will introduce how to parse Non-delivery report (NDR) in Delphi.

Read Receipt

Some e-mail applications, such as Microsoft Office Outlook, employ a read-receipt tracking mechanism. A sender selects the receipt request option prior to sending the message. Upon opening the email, each recipient has the option of notifying the sender that the message was opened and read.

However, there is no guarantee that you will get a read-receipt. Some possible reason are that very few e-mail applications or services support read receipts, or simply because users disable the functionality. Those do support read-receipt aren’t necessarily compatible with or capable of recognizing requests from a different e-mail service or application

Delivery Receipt and FailureReport

It is also called a DSN (delivery service notification), which is a request to the recipient’s email server to send you a notification about the delivery of an email you’ve just sent. The notification takes the form of an email, and will tell you if your delivery succeeded (Delivery Receipt), failed, got delayed (Failure Report).

Parse Report

For many email campaign applications, the very important task is detecting if the email is received by recipient or not. Parsing the delivery report is the common way to get the email status. EAGetMail .NET class provides a built-in function (GetReport) to parse the report. The following sample demonstrates how to parse the delivery-report.

If ReporType is DeliveryReceipt or ReadReceipt, the report probably has only OriginalSender, OriginalRecipient and OriginalMessageID information in the report, it depends on the mail server that generated the report.

Note

Remarks: All of examples in this section are based on first section: A simple Delphi project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference to your project.

[Delphi Example - Parse delivery report]

The following example codes demonstrate how to parse delivery report.

Note

To get the full sample projects, please refer to Samples section.

unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, EAGetMailObjLib_TLB;

type
    TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
    private
        { Private declarations }
        procedure ParseReport(fileName: WideString);
    public
        { Public declarations }
    end;

const
    // Report Type
    FailureReport = 0;
    DeliveryReceipt = 1;
    ReadReceipt = 2;
    Receipt_Deleted = 3;
    DelayedReport = 4;

var
    Form1: TForm1;

Implementation

{$R *.dfm}

procedure TForm1.ParseReport(fileName: WideString);
var
    oMail: TMail;
    oReport: IMailReport;
    oHeaders: IHeaderCollection;
    i: Integer;
begin
    oMail := TMail.Create(Application);
    oMail.LicenseCode := 'TryIt';

    oMail.LoadFile(fileName, false);

    if not oMail.IsReport then
        begin
            ShowMessage('This is not a report!');
            exit;
        end;

    oReport := oMail.GetReport();
    if oReport.ReportType = DeliveryReceipt then
        ShowMessage('This is a deliver receipt!')
    else if oReport.ReportType = ReadReceipt Then
        ShowMessage('This is a read receipt!')
    else if oReport.ReportType = Receipt_Deleted then
        ShowMessage('This is a unread receipt, this email was deleted without read!')
    else if oReport.ReportType = DelayedReport then
        ShowMessage('This is a delayed report, this email will be tried later!')
    else
        ShowMessage('This is a failure report!');

    // Get original message information
    ShowMessage(oReport.OriginalSender);
    ShowMessage(oReport.OriginalRecipient);
    ShowMessage(oReport.OriginalMessageID);

    if (oReport.ReportType = FailureReport) or (oReport.ReportType = DelayedReport) then
        begin
            ShowMessage(oReport.ErrCode);
            ShowMessage(oReport.ErrDescription);
            ShowMessage(oReport.OriginalSubject);
            ShowMessage(oReport.ReportMTA);
            oHeaders := oReport.OriginalHeaders;
            // Parse original email headers.
            for i := 0 to oHeaders.Count - 1 do
                begin
                    ShowMessage(oHeaders.Item(i).HeaderKey + ':' +
                        oHeaders.Item(i).HeaderValue);
                end;
        end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    try
        ParseReport('c:\my folder\test.eml');
    except
        on ep:Exception do
            ShowMessage('Error: ' + ep.Message);
    end;

end;

end.

Parse Non-Delivery Report (NDR) using EAGetMail Service

To retrieve and parse Failure Report (NDR), you should monitor your sender mailbox. Here I will introduce how to use EAGetMail Service to monitor a mailbox and retrieve non-delivery report and insert it to SQL server on a regular basis.

Install EAGetMail Service

To use EAGetMail Service, you need to download EAGetMail Service and install it on your machine at first.

Create SQL table to store report

Then create a table in your SQL database like this:

CREATE TABLE [dbo].[Failure_Report](
    [reportid] [int] IDENTITY(1,1) NOT NULL,
    [address] [nvarchar](255) NOT NULL,
    [error_code] [nchar](10) NOT NULL,
    [error_desc] [nchar](255) NOT NULL,
    [error_datetime] [datetime] NOT NULL
) ON [PRIMARY]

GO

Create a Delphi Console Application to process report

Create a Delphi console application named “parse_reports”, then

  • Select “File | New Application”
  • Select “Project | Remove From Project…”
  • Select Unit1 (Form1) and click OK. Delphi will remove the selected unit from the uses clause of the current project.
  • Select “Project | View Source”

Use the following codes to overwrite current source codes, then you get a non-GUI Delphi Application.

program parse_reports;
    {$APPTYPE CONSOLE}
uses
    SysUtils, DB, ADODB, Variants, ComObj, ActiveX, EAGetMailObjLib_TLB;

{$R *.res}

Const
    FailureReport = 0;
    DeliveryReceipt = 1;
    ReadReceipt = 2;
    Receipt_Deleted = 3;
    DelayedReport = 4;

var
    oConn: TADOConnection;
    oTools: TTools;
    files: Variant;
    i, count: integer;

function ParseEmail( fileName: Variant ):Boolean;
var
    oMail: TMail;
    oReport: IMailReport;
    oCommand: TADOCommand;
    errorDesc: WideString;
    oParam: TParameter;
    i: integer;
begin

    oMail := TMail.Create(nil);
    oMail.LicenseCode := 'TryIt';
    oMail.LoadFile(fileName, true);

    if not oMail.IsReport then
    begin
        writeln( 'not a report or receipt!');
        Result := false;
        exit;
    end;

    oReport := oMail.GetReport();
    if oReport.ReportType <> FailureReport then
    begin
        writeln( 'not a failure report!');
        Result := false;
        exit;
    end;

    writeln( 'OriginalRecipient: ' + oReport.OriginalRecipient );
    writeln( 'ErrorCode: ' + oReport.ErrCode );
    writeln( 'ErrorDesc: ' + oReport.ErrDescription );

    errorDesc := oReport.ErrDescription;

    if Length(errorDesc) > 250 then
        SetLength(errorDesc, 250);


    oCommand := TADOCommand.Create(nil);
    oCommand.Connection := oConn;
    oCommand.CommandText := 'INSERT INTO [dbo].[Failure_Report]' +
        ' ([address] ' +
        ' ,[error_code] ' +
        ' ,[error_desc] ' +
        ' ,[error_datetime]) ' +
        ' VALUES (:address, :error_code, :error_desc, GETDATE())';

    // writeln(oCommand.CommandText);
    oCommand.CommandType := cmdText;
    oCommand.Prepared := false;
    oCommand.ExecuteOptions := [eoExecuteNoRecords];
    oCommand.CommandType := cmdText;

    oCommand.Parameters.ParamByName('address').Value := oReport.OriginalRecipient;
    oCommand.Parameters.ParamByName('error_code').Value := oReport.ErrCode;
    oCommand.Parameters.ParamByName('error_desc').Value := errorDesc;
    oCommand.Execute;

    Result := true;
end;

begin


    if ParamCount() < 1 then
    begin
        writeln('Usage: parse_reports.exe [folder path]');
        writeln('e.g: parse_reports.exe "d:\my\inbox"');
        exit;
    end;

    CoInitialize(nil);
    try
        oTools := TTools.Create(nil);
        oConn := TADOConnection.Create(nil);

        // For more connection string
        // MS SQL Server 2000
        // 'Driver={SQL Server};Server=localhost; Database=myDB;Uid=myUser;Pwd=myPassword;'

        // MS SQL Server 2005
        // 'Driver={SQL Server Native Client};Server=localhost; Database=myDB;Uid=myUser;Pwd=myPassword;'

        // MS SQL Server 2005 Native Provider
        // 'Provider=SQLNCLI;Server=localhost; Database=myDB;Uid=myUser;Pwd=myPassword;'

        // MS SQL Server 2008
        // 'Driver={SQL Server Native Client 10.0};Server=localhost; Database=myDB;Uid=myUser;Pwd=myPassword;'

        // MS SQL Server 2008 Native Provider
        // 'Provider=SQLNCLI10;Server=localhost; Database=myDB;Uid=myUser;Pwd=myPassword;'

        // MS SQL Server 2012
        // 'Driver={SQL Server Native Client 11.0};Server=localhost; Database=myDB;Uid=myUser;Pwd=myPassword;'

        // MS SQL Server 2012 Native Provider
        // 'Provider=SQLNCLI11;Server=localhost; Database=myDB;Uid=myUser;Pwd=myPassword;'

        // change it to your sql server address, database, user and password
        // The server/instance name syntax used in the server option is the same for all SQL Server connection strings.
        // e.g.: Server=serveraddress\instancename;
        // open database connection

        oConn.ConnectionString := 'Driver={SQL Server Native Client 11.0};Server=localhost;Database=myDB;Uid=myUser;Password=myPassword';
        oConn.Open();

        files := oTools.GetFiles(ParamStr(1) + '\*.eml' );
        count := VarArrayHighBound(files, 1)+1;
        for i:= 0 to count - 1 do
        begin
            writeln( VarArrayGet(files, i));
            if ParseEmail(VarArrayGet(files, i)) then
            begin
                // Delete the local report file
                oTools.RemoveFile(VarArrayGet(files, i));
            end;
        end;
        oConn.Close;

    except on ep:Exception do
        begin
        writeln(ep.Message);
        end;
    end;


end.

Set a schedule to check email

Finally, open EAGetMail Service Manager -> Mail Pull Configuration -> New:

  • Input your sender mailbox account information

  • Create a folder named “inbox” on your machine, this folder is used to store .EML file.

  • Input the folder full path to “Save email file(s) to specified local folder:”;

  • Input application full path [SPACE] folder full path to: “Run specified application after download is finished”.

    For example: If your application full path is d:\parse_reports.exe and your folder is d:\inbox, then input: "d:\parse_reports.exe" "d:\inbox"

With above setting, EAGetMail Service checks mailbox every 15 minutes and once there is non-delivery report, it will invoke parse_reports.exe to process non-delivery report and insert it to database like this:

Important

If you have “Leave a copy of message on mail server” unchecked, EAGetMail Service will delete all emails in your mailbox after the emails were retrieved to local folder. If your mailbox is only used to retrieve non-delivery report, then I recommend you have “Leave a copy of message on mail server” unchecked to get better performance.

Debug your application

You can run your application directly under DOS prompt without EAGetMail Service. If there is any error, you can debug and fix it.

"d:\parse_reports.exe" "d:\inbox"

EAGetMail Service is a common solution to process email on a regular basis, you can use above solution to download and process normal emails as well. You just need to change/extend the codes in parse_reports.exe

Common SQL Driver Download

If SQL Server is installed on a remote server, and you don’t have SQL driver installed on local machine, then you need to download and install corresponding driver on local machine.

Next Section

At next section I will introduce how to manage folders with IMAP4/Exchange Web Service (EWS)/WebDAV protocol.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.