Create an SmtpReceiveAgent transport agent for Exchange 2013

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

Applies to: Exchange Server 2013

Related code snippets and sample apps:

The SmtpReceiveAgentFactory and SmtpReceiveAgent classes enable you to extend the behavior of the Front End Transport service on a Client Access Server or the Transport service on a Mailbox server. You can use these classes to implement transport agents that are designed to respond to messages as they come into your organization.

The SmtpReceiveAgentFactory and SmtpReceiveAgent classes include the events listed in the following table.

Table 1. SmtpReceiveAgent class events

Event Description
OnAuthCommand
Use when your agent requires information that is provided only in the SMTP AUTH command, such as an agent that accepts or rejects attempts to deliver a message based on the type of authentication method used.
OnConnect
Use when your agent requires information that is provided only when a connection is opened via SMTP to the Front End Transport service, such as an agent that performs an action based on the address or domain of the remote SMTP server.
OnDataCommand
Use this event when your agent requires information that is provided in the SMTP DATA command.
OnDisconnect
Use when your agent requires information that is available at the time of disconnection, such as the current date and time, in order to perform time calculations.
OnEhloCommand
Use when your agent requires information that is provided in the SMTP EHLO command; for example, if your agent accepts or rejects messages based on the identity provided in the EHLO command.
OnEndOfAuthentication
Use when your agent requires information that is available after the remote server completes the authentication process; for example, for an agent that performs an action on a message based on the authentication information provided by the remote SMTP server or client.
OnEndOfData
Use when your agent must perform an action based on data that is available in the message. This event will not fire on the Front End Transport service. If your transport agent has to use this event, you have to install it on a Mailbox server.
OnEndOfHeaders
Use when your agent must perform an action based on information that is available in the headers of the submitted message.

Creating a custom SmtpReceiveAgent transport agent

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

To create the transport agent

  1. Add references to the namespaces.

       using Microsoft.Exchange.Data.Transport;
       using Microsoft.Exchange.Data.Transport.Smtp;
       using Microsoft.Exchange.Data.Transport.Email;
       using Microsoft.Exchange.Data.TextConverters;
    
    

    You can find these namespaces on your Exchange 2013 server. When you add a reference to these namespaces, you will have access to the SmtpReceiveAgent members as well as other classes used in the Exchange 2013: Build a body conversion transport agent sample.

  2. Implement the derived class for the SmtpReceiveAgentFactory class.

       public class BodyConversionFactory : SmtpReceiveAgentFactory
       {
           /// <summary>
           /// Create a new BodyConversion
           /// </summary>
           /// <param name="server">Exchange server</param>
           /// <returns>A new BodyConversion</returns>
           public override SmtpReceiveAgent CreateAgent(SmtpServer server)
           {
               return new BodyConversion();
           }
       }
    
    

    This code will instantiate the derived class and override the CreateAgent method to create an instance of your new custom agent.

  3. Define your agent.

      public class BodyConversion : SmtpReceiveAgent
       {
           // Your custom code goes here
           /// <summary>
           /// The constructor registers an end of data event handler.
           /// </summary>
           public BodyConversion()
           {
               Debug.WriteLine("[BodyConversion] Agent constructor");
               this.OnEndOfData += new EndOfDataEventHandler(this.OnEndOfDataHandler);
           }
       }
    
    

    After you define your agent class, you can add your custom functionality. In this example, the OnEndOfData event is redirected to your custom event handler.

See also