none
Add recipient inside a message transport agent RRS feed

  • Question

  • Exchange 2010SP1 message transport agent SDK

    I'm making an MTA to automatically archive certain emails that go through the transport pipeline. Is there a way to copy an email (or add a recipient) inside a transport agent event? According to technet an agent needs to handle the OnSubmittedMessage event (in RoutingAgent) and I have that set up.

     

        public class RoutingMailAgent : RoutingAgent
        {
            private static ILog _log;
            public const string RoutedMessageRoutingKey = "vis.process.mail.{routingtype}";
    
            public RoutingMailAgent()
            {
                _log = LogManager.GetLogger(GetType());
                _log.Info("Starting");
                OnSubmittedMessage += SubmittedMessageHandler;
            }
    
            private void SubmittedMessageHandler(SubmittedMessageEventSource source, QueuedMessageEventArgs e)
            {
                try
                {
                    _log.Info("SubmittedMessageHandler START");
                    if (SubjectInfo.IsPertinent(e.MailItem.Message.Subject) == SaveMode.None)
                        return;
                    var routingType = e.MailItem.InboundDeliveryMethod.ToString();
                    var msg = new EmailMessageExt(e.MailItem.Message, e.MailItem.InboundDeliveryMethod);
                    KPICLient.SendRawMessage(RoutedMessageRoutingKey.Replace("{routingtype}", routingType), msg);
                    _log.Info("SubmittedMessageHandler END");
                }
                catch (Exception ex)
                {
                    _log.Error("SubmittedMessageHandler ERROR", ex);
                }
            }
        }
    

     

    Inside SubmittedMessageHandler I need to add logic that in some way lets the email in question (along with all data) find it's way into an archive box (separate Mailbox set up for the purpose).

    Regards, Högni


    • Edited by Hogni Tuesday, January 10, 2012 1:39 PM
    Tuesday, January 10, 2012 1:38 PM

All replies

  • This answered my question.

    When I MailItem.Recipients.Add in OnRoutedMessage it threw NotImplemented, but using it in OnSubmitted works?

    Working code:

            private static ILog _log;
            public const string RoutedMessageRoutingKey = "vis.process.mail.{routingtype}";
    
            public RoutingMailAgent()
            {
                _log = LogManager.GetLogger(GetType());
                _log.Info("Starting");
                OnSubmittedMessage += SubmittedMessageHandler;
            }
    
            private void SubmittedMessageHandler(SubmittedMessageEventSource source, QueuedMessageEventArgs e)
            {
                try
                {
                    if (SubjectInfo.IsPertinent(e.MailItem.Message.Subject) == SaveMode.None)
                        return;
                    _log.Info("SubmittedMessageHandler START");
                    var routingType = e.MailItem.InboundDeliveryMethod.ToString();
                    e.MailItem.Recipients.Add(new RoutingAddress("malaskra_mail@testvis.local"));
                    var msg = new EmailMessageExt(e.MailItem.Message, e.MailItem.InboundDeliveryMethod);
                    KPICLient.SendRawMessage(RoutedMessageRoutingKey.Replace("{routingtype}", routingType), msg);
                    _log.Info("SubmittedMessageHandler END");
                }
                catch (Exception ex)
                {
                    _log.Error("SubmittedMessageHandler ERROR", ex);
                }
            }
    


    Regards, Högni

    • Proposed as answer by T. Sweet Friday, May 18, 2012 9:45 PM
    Tuesday, January 10, 2012 3:04 PM
  • yes because onSubmitted is the first in the pipeline. on routed is the last.
    Thursday, February 16, 2012 6:18 AM
  • Hi,

    Any chance you could explain what this bit of code does?

    var msg = new EmailMessageExt(e.MailItem.Message, e.MailItem.InboundDeliveryMethod);
                    KPICLient.SendRawMessage(RoutedMessageRoutingKey.Replace("{routingtype}", routingType), msg);

    I need to make a copy of every single outgoing email to a certain mailbox and am hoping to use something similar to your code.

    Thanks


    IT Support/Everything

    Monday, November 26, 2012 11:20 PM