Parse Non-Delivery Report (NDR) in VB.NET

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 VB.NET.

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 VB.NET 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.

[VB.NET 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.

Imports System.Text
Imports System.IO
Imports EAGetMail 'imports EAGetMail namespace

Module Module1
    Private Sub ParseReport(ByVal emlFile As String)
        Dim oMail As New Mail("TryIt")
        oMail.Load(emlFile, False)

        If Not oMail.IsReport Then
            Console.WriteLine("This is not a delivery report.")
            Return
        End If

        Dim oReport As MailReport = oMail.GetReport()
        Select Case oReport.ReportType
            Case DeliveryReportType.DeliveryReceipt
                Console.WriteLine("This is a delivery receipt!")
                Exit Select
            Case DeliveryReportType.ReadReceipt
                Console.WriteLine("This is a read receipt!")
                Exit Select
            Case DeliveryReportType.Deleted
                Console.WriteLine("This is a unread receipt, this email was deleted without read!")
                Exit Select
            Case DeliveryReportType.DelayedReport
                Console.WriteLine("This is a delayed report, server will retry to send the email later automatically!")
                Exit Select
            Case Else
                Console.WriteLine("This is a failure report!")
                Exit Select
        End Select

        Console.WriteLine("OriginalSender: {0}", oReport.OriginalSender)
        Console.WriteLine("OriginalRecipient: {0}", oReport.OriginalRecipient)
        Console.WriteLine("OriginalMessageID: {0}", oReport.OriginalMessageID)

        If oReport.ReportType = DeliveryReportType.FailureReport Or
            oReport.ReportType = DeliveryReportType.DelayedReport Then
            Console.WriteLine("ErrCode: {0}", oReport.ErrCode)
            Console.WriteLine("ErrDescription: {0}", oReport.ErrDescription)
            Console.WriteLine("OriginalSubject: {0}", oReport.OriginalSubject)
            Console.WriteLine("ReportMTA: {0}", oReport.ReportMTA)
            Console.WriteLine(oReport.OriginalHeaders.ToString())
        End If
    End Sub

    Sub Main()
        Try
            ParseReport("c:\my folder\test.eml")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

    End Sub
End Module

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 VB.NET Console Application to process report

Create a VB.NET console application named “parse_reports”, then

Input the following codes:

Imports System.Data.SqlClient
Imports EAGetMail
Imports System.IO

Module Module1

    Sub Main(ByVal args() As String)
        If (args.Length < 1) Then
            Console.WriteLine("Usage: Parse_Reports.exe [email folder path]")
            Console.WriteLine("eg: Parse_Reports.exe ""c:\my folder""")
            Exit Sub
        End If

        Try
            ' 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
            Dim oConn As New SqlConnection("Server=localhost;Database=myDB;User Id=myUser;Password=myPassword;")
            oConn.Open()

            Dim files() As String = Directory.GetFiles(args(0), "*.eml")
            Console.WriteLine("Total {0} email(s)", files.Length)
            Dim count As Integer = files.Length
            For i As Integer = 0 To count - 1

                Dim fileName As String = files(i)
                If (ParseEmail(fileName, oConn)) Then
                    ' Delete the local report file.
                    File.Delete(fileName)
                End If
            Next

            oConn.Close()

        Catch ep As Exception

            Console.WriteLine(ep.Message)
            Console.WriteLine(ep.StackTrace)
        End Try
    End Sub

    Function ParseEmail(ByVal fileName As String, ByRef oConn As SqlConnection)
        ParseEmail = False
        Dim oMail As New Mail("TryIt")
        oMail.Load(fileName, True)

        ' detect if this is a report or receipt
        If (Not oMail.IsReport) Then

            Console.WriteLine("Not a report or receipt!")
            Exit Function
        End If

        Dim oReport As MailReport = oMail.GetReport()
        ' we only process failure report
        If (oReport.ReportType <> DeliveryReportType.FailureReport) Then

            Console.WriteLine("Not a failure report!")
            Exit Function
        End If

        Console.WriteLine("OriginalRecipient: {0}", oReport.OriginalRecipient)
        Console.WriteLine("ErrorCode: {0}", oReport.ErrCode)
        Console.WriteLine("ErrorDesc: {0}", oReport.ErrDescription)
        Dim errorDesc As String = oReport.ErrDescription
        If (errorDesc.Length > 250) Then
            errorDesc = errorDesc.Substring(0, 250)
        End If

        ' INSERT the result to database.
        Dim sql As String = "INSERT INTO [dbo].[Failure_Report] " & _
            " ([address] " & _
            " ,[error_code] " & _
            " ,[error_desc] " & _
            " ,[error_datetime]) " & _
            " VALUES ( @address, @error_code, @error_desc, GETDATE())"

        Dim command As New SqlCommand(sql, oConn)
        command.Parameters.AddWithValue("@address", oReport.OriginalRecipient)
        command.Parameters.AddWithValue("@error_code", oReport.ErrCode)
        command.Parameters.AddWithValue("@error_desc", errorDesc)

        command.ExecuteNonQuery()
        command.Dispose()
        ParseEmail = True

    End Function

End Module

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.