Create a DeliveryAgent transport agent for Exchange 2013

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

Applies to: Exchange Server 2013

The DeliveryAgentFactory<Manager> and DeliveryAgent classes are the base classes for transport agents that are designed to run on the Transport service on an Exchange Server 2013 Mailbox server. You might implement handlers in your DeliveryAgent transport agent for the events provided by the DeliveryAgent class that are listed in the following table.

Table 1. DeliveryAgent class events

Event Description
OnCloseConnection
Occurs after the last mail item has been delivered and the connection is closed.
OnDeliverMailItem
Occurs when a mail item is ready to be delivered.
OnOpenConnection
Occurs when the delivery agent is opened for mail delivery.

Creating a custom DeliveryAgent transport agent

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

To create the transport agent

  1. Add references to the namespaces.

         using Microsoft.Exchange.Data.Transport;
         using Microsoft.Exchange.Data.Transport.Delivery;
    
    

    You can find these namespaces on your Exchange server. By adding a reference to these namespaces, you will have access to the DeliveryAgent members.

  2. Implement the derived class for the DeliveryAgentFactory<Manager> class.

       public class MyDeliveryAgentFactory : DeliveryAgentFactory<MyDeliveryAgentFactory.MyDeliveryAgentManager>
       {
           static MyDeliveryAgentFactory()
           {
           }
           public override DeliveryAgent CreateAgent(SmtpServer server)
           {
               return new MyDeliveryAgent(server);
           }
           public sealed class MyDeliveryAgentManager : DeliveryAgentManager
           {
               /// <summary>
               /// Gets the supported delivery protocol.
               /// </summary>
               public override string SupportedDeliveryProtocol
               {
                   get { return "MyProtocol"; }
               }
           }
       }
    
    

    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. A DeliveryAgentManager class is created to override the SupportedDeliveryProtocol property and set the protocol your agent will use.

  3. Define your agent.

       public class MyDeliveryAgent : DeliveryAgent
       {
           public MyDeliveryAgent(SmtpServer server)
           {
               this.OnCloseConnection += CloseConnection;
               this.OnDeliverMailItem += DeliverMailItem;
               this.OnOpenConnection += OpenConnection;
           }
       }
    
    

    After you define your agent class, you can add you custom functionality. In this example, the three events, OnCloseConnection, OnDeliverMailItem, and OnOpenConnection, are redirected to your custom event handlers.

See also