Exchange Server Event Sink in c#

I’ve been asked to make some minor modifications to a component I developed some time ago. Reviewing the code, I thought it would be a nice topic to talk about it.

This component is an Event Sink which is supposed to be installed in an Exchange Server that parses all incoming email messages (depending on the rules defined on it’s registration, but this is a topic I’ll leave for another post!) and check if they contain some tag in it’s subject field. If so, it does some processing and changes their subject by adding a #OK or #ERROR tag depending on the process outcome. This tag is meant to be used by the Outlook to determinate on which folder should the message be placed (defined by some Outlook rule by the client)

What we need in order to create an Exchange event sink is a class that implements an interface defined in th COM+ component cdoex.dll which we can find in the bin folder of our exchange server. In order to do that we must build the interop .net assembly that will wraps us this library in managed code. We can do that with the following call to tlbimp

sn –k sn.key 
tlbimp cdoex.dll /keyfile:sn.key /out:Interop.cdoex.dll /namespace:CDO

Then we add this reference to our assembly and creates a class that implements the ISMTPOnArrival interface. There are a lot of other interfaces in the cdoex library depending on when do we want our event sink to be executed, for this example I needed to be when a message arrived and this is why I choosed this particular interface. Here is the class declaration:

[Guid("021079E3-6FCB-491b-A78F-81BC31A1EC9D")]
[ComVisible(true)]
public class MyEventSink : ISMTPOnArrival, IEventIsCacheable

So following is the implementation of the ISMTPOnArrival interface:

void ISMTPOnArrival.OnArrival(IMessage Msg, ref CdoEventStatus EventStatus)
{
    try
    {
        //Do message processing
        //Here we can access all message's properies
        //for example the message body: Msg.TextBody

        //Change subject to OK
        Msg.Fields["urn:schemas:mailheader:subject"].Value = "#OK# " + Msg.Subject;

    }
    catch (Exception)
    {
        //Change subject to ERROR
        Msg.Fields["urn:schemas:mailheader:subject"].Value = "#ERROR# " + Msg.Subject;
    }
    finally
    {
        //Save message properties
        Msg.Fields.Update();
        Msg.DataSource.Save();
        EventStatus = CDO.CdoEventStatus.cdoRunNextSink;
    }
}

Then, all we need to do is register the .dll in the Exchange server but this is a topic I’ll cover in another post ! As always, feel free to leave any comment !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: