Writing Managed Sinks for SMTP and Transport Events


Summary: This document provides developers an overview of how to write event sinks for SMTP and Transport events in managed code using wrappers that obscure some of the details of communicating with the unmanaged server.


Introduction


Writing event sinks in managed code allows the programmer to make use of the Microsoft® .NET Framework and to more efficiently write the code that is necessary for the sink. Writing managed sinks can be somewhat difficult, however, because event sink interfaces were designed primarily for C++ programmers and are therefore not very easy to use in a language such as C#. In addition, some of the methods that are imported using Tlbimp.exe will not work unless they are modified at the intermediate language (IL) level. However, these inconsistencies in the imported assemblies have been fixed in the events' primary interop assembly (PIA) provided, and other interfaces that were inconvenient to use have also been wrapped so that they are easier to use from managed code.


These managed wrappers wrap each of the methods on the original interfaces and correctly communicate with the unmanaged server. In addition, some of the original methods are exposed as properties instead of pairs of methods. For example, all pairs of Set and Query methods are exposed as properties.


This document assumes that the reader has a working knowledge of the .NET Framework, COM, COM Interop and Microsoft Windows® 2000 SMTP Service Events.


For information about event sinks in general, see Microsoft Windows 2000 SMTP Service Events. As described in that document, sinks can implement a number of interfaces to handle corresponding events on the server. The methods of those interfaces are then called when the appropriate event is triggered and certain parameters are passed in.


How to Write a Managed Event Sink in C#: Step by Step


To write a managed event sink, you must link to the PIA that contains the necessary interfaces and implement the interfaces that correspond to the events that need to be handled. Optionally, you can link to the assembly that contains the easier-to-use wrappers for these interfaces.


Read the rest on MSDN web site...


Pretish Abraham

Comments (12)
  1. Chris says:

    This is a reprint of an MSDN article which is over a year old. It links to a 4 year old article. Not that either of these articles is bad, but most people with a strong interest in writing event sinks will have already seen these. There’s not a whole lot of documentation on event sinks which isn’t contained either in this article or the links which spread from it.

    For those who want more in depth documentation, where do we turn? "Programming Microsoft Outlook and Microsoft Exchange 2003" only manages to devote 3 pages to the subject.

  2. Pretish Abraham says:

    Chris, is there anything in particular you would like more documentation about? Ill try digging up something.

  3. Chris says:

    How about a something on writing your own protocol verb both publishing (EHLO) and sending?

  4. mike says:

    Has anyone tried the Managed Sinks implementation lately? I tried the step by step implementation on Windows 2003 Server just yesterday. Everything registers both in the COM (by Vstudio) and Events (using smtpreg.vbs) bases , the Managed COM object can instantiate, but alas the SMTP Virtual Server never calls it. Further the inetinfo.exe process never appears to load the assembly containing the event sink. The only thing I see that is weird is that the COM Object viewer does not show the Sink as having implemented ISmtpInCommandSink. Could the Interop DLL provided by the article with the patch have some bugs in it? Any suggestions greatly appreciated.

  5. Nino Bilic says:

    Mike,

    I have looked and asked around and also checked with the article author etc… we are not aware of any problems or bugs that would explain what you are seeing. I’d ask you to open up a case with out messaging dev support people to look into this as it is not expected. It is hard to give you more details without asking for a lot more detail :)

  6. There are definitely some steps missing from the documentation in order to get from building the source into a working system. Perusal of the exchange.development newsgroup reveals that you need to do "regasm /codebase" to get the DLLs installed into the GAC.

    Also, the documentation could do with expanding on the other interfaces. For example, is it possible to write a managed sink for the transport interface? If so, how – because the parameters are very different from the Inbound & Outbound sinks.

  7. Also, can we have more details on what you can do with the context, e.g. how do you tell the outbound SMTP sink to not send the message that you are currently looking at?

    Or is this documented somewhere else and it needs cross-referencing from this document?

  8. prasad says:

    Hi,

    I have read the article yesterday itself and trying to implement it…still couldn’t do it.

    I couldn’t see nmake.exe in ManagedSinksWPInterop Folder.

    I am trying to create a sink which will create a copy of every email message that passes through the sink and saves it to a file on disk (say in a "c:temp").

    Did any one have done something like this..if so help needed.

    Thanks

    prasad

  9. mike says:

    Nino: Thanks for your investigation. I got back to working on it today and finally reinstalled IIS and the SMTP service, once I did this and re-registered the event sink assembly, everything is working as advertised in article. So I can vouch that the article is still relevant and current. Thanks everyone for the input. – Mike

  10. nigelw says:

    Pretish, you mentioned that you might be able to dig some information up? That is exactly what I need.

    It would be really nice to get complete documentation on all of the interfaces declared in mailmsg.idl, smtpevent.idl and seo.idl. I’ve tried guessing functionality from the idl files, but you can only go so far with this.

    Specific question is this: In a sink, how do I create new mailmsg objects to feed into the SMTP service? (I want to be able to split messages with multiple recipients into individual messages for per-user processing purposes).

    Any help would be greatly appreciated. Thanks.

  11. NeedSmtpOutHelp says:

    I would like to see some kind of clean sample that wires-up to specific SMTP codes when Exchange is the sender. For example, when Exchange SmtpSvc sends to a remote SMTP server and gets a 501, I would like to see that event trapped/handled in C#.

    All of the samples out in the wild only deal with OnSmtpArrival.

  12. Martijn Thie says:

    To all who need more information on writing managed event sinks (for IIS SMTP or Exchange), please take a look at my blog.

    http://blog.rednael.com/

    There is a very in depth article on How to write a custom authentication sink (using the AUTH command). It addresses various problems that are solved when writing event sinks.

    For example, how do you read input lines after the AUTH command is processed.

    And how do you use the property bags within a session.

    And more…

    Also, there is a list of registry settings for IIS SMTP (as well as Exchange 2000/2003), with which you can influence the behavior of the SMTP server.

    Enjoy…

Comments are closed.

Skip to main content