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

Posted on 2004-11-23
Last Modified: 2008-01-09
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

Question by:PHolroyd

    Author Comment

    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.

    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now