Recently I had the task of building a transport agent for MS Exchange 2007 that reads each incoming e-mail, looks for a specific string, and modifies the original subject if the string is found. I used c# in .net 2.0 to accomplish this task with only a few lines of code. I will do my best to walk you through the steps to accomplish looking at each e-mail that is received in Exchange.
In VS 2005 or 2008 create a Class Library solution type.
You will need to reference the following assemblies from the Exchange Server:
Microsoft.Exchange.Data.Common and Microsoft.Exchange.Data.Transport (They are both found at C:\Program Files\Microsoft\Exchange Server\Public)
Create a class file in your solution, for this example I will call my class file Agent.cs. Inside Agent.cs you will need to write 2 classes, one class, a sealed factory, will inherit from the RoutingAgentFactory class and will need to implement the following method:
CreateAgent(Microsoft.Exchange.Data.Transport.SmtpServer server)
The other class will inherit from the RoutingAgent class and will contain a constructor and an event handler to catch the OnRoutedMessageEvent. Here is what your constructor will look like assuming my class name is Agent:
Agent{
this.OnRoutedMessage += new RoutedMessageEventHandler(Agent_OnRoutedMessage);
}
and the event handler will contain the code to check the subject line
Agent_OnRoutedMessage(RoutedMessageEventSource source, QueuedMessageEventArgs e){
//add code here, you can access the e-mail message by using e.MailItem
//This line would add the current date to the end of the subject line
e.MailItem.Message.Subject += DateTime.Now.ToString();
}
One final line of code to add goes in the CreateAgent function in the factory class…
override RoutingAgent CreateAgent(Microsoft.Exchange.Data.Transport.SmtpServer server){
return new Agent();
}
Installing the transport only takes a couple of commands using the Exchange Management Shell, I will post instructions soon.