Link to home
Start Free TrialLog in
Avatar of PHolroyd
PHolroyd

asked on

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()
        MyBase.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.

Cheers

P Holroyd


Avatar of PHolroyd
PHolroyd

ASKER

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 :-

http://support.microsoft.com/?id=315631

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"
        Msg.DataSource.Save()

        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.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial