Brains Coders

Brains Engineering Blog

Logging delivery failures on HMailServer

HMailServer has the possibility to attach a script to its major events, delivery accepted, failure, server error, etc.

In order to track delivery failure, like mailbox unavailable or SPAM rejection here is a script I've written to use in the OnDeliveryFailed event:

Function FailDeliveryLogToDB(oMessage, sRecipient, sErrorMessage)
Dim Conn
Dim strConn
Dim strSql
Dim bounceMsg, bounceBody, headers, rId, lId, sId, sbId, iHeader
Dim strUid
Dim strMsgId
Dim deliveryDate
Dim arr
Dim iMonth
Dim errCode
For iHeader = 0 To oMessage.Headers.Count - 1
If (oMessage.Headers(iHeader).Name = "X-UID") Then
strUid = oMessage.Headers(iHeader).Value
strUid = Replace(Replace(strUid,"<",""),">","")
End If
If (oMessage.Headers(iHeader).Name = "Message-ID") Then
strMsgId = oMessage.Headers(iHeader).Value
End If
arr = split(oMessage.Date," ")
iMonth = Month(DateValue("01-" & arr(1) & "-2005"))
deliveryDate = arr(2) & LPad(iMonth,2,"0") & LPad(arr(0),2,"0") & " " & arr(3)
errCode = Trim(Mid(sErrorMessage,InStr(1,sErrorMessage,"Remote server replied:")+23,3))

Set Conn = CreateObject("ADODB.Connection")
strConn="DRIVER={SQL Server};SERVER=serverIP;UID=DbUsername;PWD=serverPwd;DATABASE=DatabaseName" strConn
strSql = "INSERT INTO [DeliveryFailures] ([Uid],[ErrorDescription],[Recipient],[Sender],[MsgId],[ErrorCode],[DeliveryDate]) VALUES  ('" & strUid & "','" & sErrorMessage & "','" & sRecipient & "','" & oMessage.FromAddress & "','" & strMsgId & "','" & errCode & "','" & deliveryDate & "')"
If Conn.State = adStateOpen then
End If
Set Conn = Nothing

End Function

Function LPad(s, l, c)
  Dim n : n = 0
  If l > Len(s) Then n = l - Len(s)
  LPad = String(n, c) & s
End Function

Please note that in our Mailing List application, we are using a custom header field, X-UID, to track email.

This is important to generate reports on sending, failures, if some ISP has put us on a black list, etc.

If you get some error in the script execution, in particular on the CreateObject function, try to give right permission on DCOM

  1. Start DCOM config by selecting Start, Run, enter dcomcnfg.exe and press OK.
  2. In the Component Services program, expand the Component Services folder
  3. Expand down to Computers | My Computer | DCOM Config
  4. Right-click on hMailServer and select properties
  5. Select the Security tab
  6. Under "Launch and Activation Permissions", select Customize and click on Edit
  7. Under "Group or user names", click Add
  8. For IIS6: add the built-in anonymous IIS user account
  9. For IIS7 >> add the builtin IIS_IUSRS user account
  10. For Apache: add the Apache user account
  11. Set the Local Launch and Local Activation permissions for this user to Allow

I've taken the idea from here:

Comments (2) -

  • rtu1kP5I

    3/15/2018 6:11:36 PM | Reply

    763133 773875I like this  weblog  so significantly,  saved to my bookmarks . 887552

  • ZiFWqDKhULPuu

    5/24/2018 9:16:18 AM | Reply

    577133 95085I discovered your blog website on google and check just a couple of of your early posts. Proceed to preserve up the outstanding operate. I just extra up your RSS feed to my MSN Info Reader. Seeking forward to reading much more from you in a whilst!� 375476