Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

Problems with VB.NET COM Class Implementing an SMTP OnArrival event sink

Phew, Long title eh.

Basically I need to intercept Emails using the SMTP Sevice and the OnArrival event sink. I've come up with the following simple test app :-

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class
    ' and its COM interfaces. If you change them, existing
    ' clients will no longer be able to access the class.
#End Region

    ' A creatable COM class must have a Public Sub New()
    ' with no parameters, otherwise, the class will not be
    ' registered in the COM registry and cannot be created
    ' via CreateObject.

    Public Sub New()
    End Sub
    Public Overridable Sub OnArrival(ByVal Msg As CDO.Message, ByRef EventStatus As CDO.CdoEventStatus)

        EventStatus = CDO.CdoEventStatus.cdoSkipRemainingSinks
        Dim sr As StreamWriter = File.AppendText("C:\Logfile.txt")

        sr.WriteLine("Event Sink fired")

    End Sub

End Class

I tick the 'Register for COM Interop' box under properties->configuration properties->build and DLL seems to compile ok with no errors.

I then registered the COM Class using Regasm :-

Regasm ClassLibrary.dll /codebase

and finally point the SMTP service to the event sink with the smtpreg script provided by microsoft  :-

cscript smtpreg.vbs /add 1 OnArrival ClassLibrary ClassLibrary.ComClass "mail from=*"

The code in the event sink isn't run and nothing appears in the log file, but the smtp service does seem to open the DLL and use it because I can't access the DLL file after I send a test message to the smtp service until I reboot.

I assuming that my DLL is getting called but for some reason nothing happens after that. Can anyone see what I'm doing wrong?

I realise this is a tough one so I've assigned max points to the question.


P Holroyd

1 Solution
PHolroydAuthor Commented:
I managed it myself in the end, I'll post it as lots of people seem to be having this difficulty with VB.NET and SMTP Event Sinks

It's a similar solution to the one found in this article :-


Only you create a .NET Library File instead of the ActiveX DLL. I then Entered this simple test code :-

Imports CDO

Public Class MyOnArrival
    Implements ISMTPOnArrival

    Sub OnArrival(ByVal Msg As Message, ByRef EventStatus As CdoEventStatus) Implements ISMTPOnArrival.OnArrival

        Msg.TextBody = Msg.TextBody & "Intercepted"

        EventStatus = CdoEventStatus.cdoRunNextSink

    End Sub
End Class

Then instead of regsrvr32 you use regasm which is the .NET equivalent. For Example :-

regasm ClassLibrary.DLL /codebase

You then use the smptreg.vbs script to add your Library as an event sink on the server(Found in VS.NET and the Exchange Server SDK). For Example :-

cscript smtpreg.vbs /add 1 OnArrival ClassLibrary ClassLibrary.MyOnArrival "mail from=*"

I've put the DLL in the same directory the SMTP service sits in, but I don't know if it actually needs to there or not.

This now seems to work and incoming Emails are intercepted.

The DLL is still not accesable after the code has been called and run once though, making it difficult to ammend the code, recompile and copy it to the SMTP service directory without a reboot. So if anyone's got a soloution to this I'd be greatful.
PAQed with points refunded (500)

Community Support Moderator

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now