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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.