none
Transport agent causing poison messages RRS feed

  • Question

  • Hello,

    I am testing out a custom transport agent on a exchange hub 2007 sp1 box (enterprise 2008).

    It should route any outgoing email from "domain1" to a smarthost.  It should leave all incoming mail and outgoing mail from "domain2" alone

    The problem is that all incoming mail to "domain1" is ending up in the poison queue.  I have no clue why this is happening. 

    Here is what I have so far:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    using Microsoft.Exchange.Data.Transport;
    using Microsoft.Exchange.Data.Transport.Email;
    using Microsoft.Exchange.Data.Transport.Smtp;
    using Microsoft.Exchange.Data.Transport.Routing;
    using Microsoft.Exchange.Data.Common;

    namespace CustomTransportAgent
    {
        public class SampleRoutingAgentFactory : RoutingAgentFactory
        {
            public override RoutingAgent CreateAgent(SmtpServer server)
            {
                RoutingAgent myAgent = new ownRoutingAgent();

                return myAgent;
            }
        }
    }
    public class ownRoutingAgent : RoutingAgent
    {
        public const string LOG_FILE_PATH = @"C:\temp\MyRoutingAgent.log";

        public ownRoutingAgent()
        {
            //subscribe to different events
           
            base.OnSubmittedMessage += new SubmittedMessageEventHandler(ownRoutingAgent_OnSubmittedMessage);
            base.OnResolvedMessage += new ResolvedMessageEventHandler(ownRoutingAgent_OnResolvedMessage);
        }

        void ownRoutingAgent_OnSubmittedMessage(SubmittedMessageEventSource source, QueuedMessageEventArgs e)
        {
            // Don't process EVERY message...
            if ((e.MailItem.FromAddress.DomainPart.ToString().ToLower() == "domain1.com")  

                && !(e.MailItem.FromAddress.LocalPart.ToString().ToLower().Contains("user1"))
                && !(e.MailItem.FromAddress.LocalPart.ToString().ToLower().Contains("user2")))
            {
                // If we have multiple recipients, fork the message to process each recipient seperately...
                if (e.MailItem.Recipients.Count > 1)
                {

                    int count = 0;
                    while (e.MailItem.Recipients.Count > 1)
                    {
                        // Create an individual forked message for each recipient...
                        EnvelopeRecipient recip = e.MailItem.Recipients[e.MailItem.Recipients.Count - 1];
                        List<EnvelopeRecipient> recips = new List<EnvelopeRecipient>();
                        recips.Add(recip);
                        source.Fork(recips);
                        count++;
                    }

                }
                else
                {
                    LogMessage("OnSubmittedMessage", e.MailItem, "Single recipient, no forking needed.");
                }

            }
        }


        void ownRoutingAgent_OnResolvedMessage(ResolvedMessageEventSource source, QueuedMessageEventArgs e)
        {
            try
            {
                if (e.MailItem.FromAddress.DomainPart.ToString().ToLower() == "domain1.com")
                {
                    if (e.MailItem.Recipients.Count == 1)
                    {
                        foreach (EnvelopeRecipient recp in e.MailItem.Recipients)
                        {

                            //do not re route internal emails to smarthost
                            if (!(recp.Address.DomainPart.ToString().Contains("domain1.com"))
                                || !(recp.Address.DomainPart.ToString().Contains("domain2.com")))
                            {
                                RoutingDomain myRoutingOverride = new RoutingDomain("nexthopdomain.com");
                                recp.SetRoutingOverride(myRoutingOverride);
                                //e.MailItem.Message.Subject += recp.Address.DomainPart.ToString();
                            }
                        }
                    }
                }
            }

            catch // (Exception except)
            {

            }
        }
        private void LogMessage(string eventName, MailItem item, string message)
        {
            TextWriter tw = System.IO.File.AppendText(LOG_FILE_PATH);
            tw.WriteLine(DateTime.Now.Ticks + "\t" + eventName + " - " + item.Message.Subject + " - " + message);
            tw.Close();
        }


    }

    Any help is appreciated!

     

    Thanks,


    Greg

    Tuesday, August 23, 2011 3:20 PM