Exchange Routing Agent Detect Email Direction

879 views Asked by At

We have an Exchange Trasport Agent, which is specifically a RoutingAgent. In the OnSubmittedMessage event we need to inspect the MailItem and determine if it is outbound or inbound. I am currently looking at using the SmtpServer.AcceptedDomains property to check if the sender email is in the accepted domains list. Here is an example of how we are trying to do this:

private bool IsOutbound(MailItem mailItem, AcceptedDomainCollection acceptedDomains)
{
    if (acceptedDomains.Find(mailItem.FromAddress) != null)
    {
        return true;
    }
    return false;
}

I am not extremely familiar with Accepted Domains, and I am trying to understand if this is a viable solution to reliably determine the direction of an email? This scenario also needs to work for companies that have multiple domains included in their Exchange organization setup. Note that we plan to support Exchange 2010-2016 if that makes any difference.

--EDIT--

After getting some help from @GlenScales I realize I need to update my question a bit.

My goal is to determine if the sender of the email is in a domain that is internal to the Exchange organization. This could also be achieved by determining if the email was generated within Exchange, we are able to inspect the recipients at a later stage just fine and see if it is inbound or outbound.

With that being said, we looked at 3 possible solutions:

  1. Check the sender domain against the list of Accepted Domains
  2. Use the AddressBook to check if the sender is internal
  3. Check the InboundDeliveryMethod property and see if it was created from a mailbox

In the end we decided that we really wanted to be inspecting messages that were generated from a mailbox only and went with #3. #1 sounded like it could potentially include domains that were really just "forwarding" domains, and we do not want to consider those messages in our application.

1

There are 1 answers

5
Glen Scales On BEST ANSWER

AcceptedDomains tells you all the SMTP domains that the Exchange Organization will delver for. Eg When the Exchange Transport Server performs message categorization on a Message if the Message is To an Address that is within the Accepted domains it will take responsibility for delivery that message to that recipients (there can be multiple recipients in a message so that point the message maybe forked and delivered to the Internal recipient and the sent on to the external recipient).

What your doing just tells you where the Sender is from not really the direction of the Message as such. OnSubmittedMessage means you capturing a Message before any categorization has taken place in the case it's a message that has been sent from within your org but if the message has both internal and external recipients some copies the it will have multiple destinations.

There also some other options in https://social.msdn.microsoft.com/Forums/en-US/9387e62a-76d7-4340-b9cc-f87ffcfab8b1/how-to-detect-the-message-source-in-an-exchange-server-2013-transport-agent-for-getting-the-sending?forum=exchangesvrdevelopment which I'd suggest you look at

Cheers Glen