How to: Create a RoutingAgent transport agent for Exchange 2013

Find out how to create a custom RoutingAgent transport agent to use with Exchange 2013.

Last modified: January 10, 2014

Applies to: Exchange Server 2013

Related code snippets and sample apps  Exchange 2013: Build a bandwidth logging transport agent

The RoutingAgentFactory and RoutingAgent classes are the base classes for transport agents that are designed to run on the transport service on an Exchange Server 2013 Mailbox server. The RoutingAgent class provides the events listed in the following table for which you might implement handlers in your RoutingAgent transport agent.

Table 1.  RoutingAgent class events




Occurs after the server performs content conversion, if it is required.


Occurs after all the recipients of the message have been resolved and before routing is determined.


Occurs after the server routes the message to the next hop and performs content conversion, if required. The server might use more resources to process each message in the OnRoutedMessage event than the OnSubmittedMessage event because the server will perform any necessary content conversion and determine the next hop in the route for the message before it executes the code in the OnRoutedMessage event handler.


Occurs after the message is taken off the submit queue. Use the OnSubmittedMessage event if your RoutingAgent transport agent does not require content conversion, resolved recipients, or routing data.

The following procedure describes how to create a custom RoutingAgent transport agent.

To create the transport agent

  1. Add references to the namespaces.

        using Microsoft.Exchange.Data.Mime;
        using Microsoft.Exchange.Data.Transport;
        using Microsoft.Exchange.Data.Transport.Routing;

    You can find these namespaces on your Exchange server. By adding a reference to these namespaces, you will have access to the RoutingAgent members as well as other classes used in the Exchange 2013: Build a bandwidth logging transport agent sample.

  2. Implement the derived class for the RoutingAgentFactory class.

        public class BandwidthLoggerFactory : RoutingAgentFactory
            public override RoutingAgent CreateAgent(SmtpServer server)
                return new BandwidthLogger(server);

    This code will instantiate the derived class and override the CreateAgent method to create an instance of your new custom agent. Additional methods, such as Close, can also be overridden in this class to execute custom code.

  3. Define your agent.

        public class BandwidthLogger : RoutingAgent
            // Your custom code goes here
            public BandwidthLogger(SmtpServer server)
                Debug.WriteLine(logPrefix + "Agent constructor");
                this.server = server;
                this.OnSubmittedMessage += SubmittedMessage;
                this.OnRoutedMessage += RoutedMessage;

    After you define your agent class, you can add you custom functionality. In this example, the two events, OnSubmittedMessage and OnRoutedMessage, are redirected to your custom event handlers.