HomeBrew: Exchange Agent
Wednesday, August 27, 2008 17:39 In Exchange 2007 you have the possibility to write your own SMTP Receive Agents, and install them on a Hub Transport server. These agents will have to listen to one (or more) of these events;
- OnAuthCommand
- OnConnect
- OnDataCommand
- OnDisconnect
- OnEhloCommand
- OnEndOfAuthentication
- OnEndOfData
- OnEndOfHeaders
- OnHeloCommand
- OnHelpCommand
- OnMailCommand
- OnNoopCommand
- OnRcptCommand
- OnReject
- OnRsetCommand
All these events are explained on MSDN in this document. Â
 After writing an agent, and compiling it, you will need to tell Exchange 2007 to actually use the agent. This means installing the Agent into Exchange and enabling it.
Here’a a small HowTo and example of a simple SMTP Receive Agent that can be installed on an Exchange 2007 Hub Transport Server.First of all, we need to create the new Exchange Agent.
So let’s fire up Visual Studio (Express) and create a new Project. The target would be a class library. I’m using C# in my example.
Our first step in creating the Agent is to add references to the required DLL’s. The DLL’s we’re required to use are Microsoft.Exchange.Data.Common.dll and Microsoft.Exchange.Data.Transport.dll. Most probably you won’t have these DLL’s on your workstation, so just copy them over from the Exchange 2007 server. After creating the references in your project, add them to your code;
using Microsoft.Exchange.Data.Transport;
using Microsoft.Exchange.Data.Transport.Smtp;
using Microsoft.Exchange.Data.Common;
After that, create the eventhandlers for the events you want to bind to. In my examples I named the class MyAgent in the MyAgents namespace, but ofcourse you’re free to name it whatever you like;Â
namespace MyAgents
{
public sealed class MyAgentFactory : SmtpReceiveAgentFactory
{
public override SmtpReceiveAgent CreateAgent(SmtpServer server)
{
return new MyAgent();
}
}
public class MyAgent : SmtpReceiveAgent
{
public MyAgent()
{
this.OnEndOfData += new EndOfDataEventHandler(MyEndOfDataHandler);
}
private void MyEndOfDataHandler(ReceiveMessageEventSource source, EndOfDataEventArgs e)
{
e.MailItem.Message.Subject = "This message passed through my agent: " + e.MailItem.Message.Subject;
}
}
}
This example binds to the OnEndOfData event and prepends the subject of the message that just passed with a string.
Save the file, and compile it. After compilation, copy the resulting DLL file to the Exchange 2007 Hub Transport server.
Now, from the Exchange 2007 Hub Transport server, we need to tell Exchange to use this agent. We can do this from PowerShell. First, we need to install it, and then enable it. After that, we need to restart the Exchange Transport Service;
Install-TransportAgent -Name MyAgent -AssemblyPath C:\MyAgent.DLL -TransportAgentFactory MyAgents.MyAgentFactory
Enable-TransportAgent -Identity MyAgent
service MSExchangeTransport stop
service MSExchangeTransport start
That’s it.
If mailflow stops, you might have a fault in your agent. You can disable the agent by using the Disable-TransportAgent PowerShell command.
Leave a Reply
You must be logged in to post a comment.
