Hub Transport Rule based on recipient’s domain

If you have played long enough with Hub Transport rules in Microsoft Exchange 2007 you might have noticed that they contain no flexible condition that applies on the recipient’s address.

For whatever reason it is always assumed that the recipient is a user inside your organization and you won’t find an easy way to implement the missed ToAddressContains predicate.

In this example I will show you how to build your own ToAddressContains predicate and create a Hub Transport Rule that will Blind Carbon Copy all emails sent to @domain.com to a specific mailbox, this mailbox will be called Alerts.

Microsoft Exchange 2007 up to SP1 Rollup update 8

If you are running any version of Exchange 2007 up to SP1 Rollup 8 create your transport rule by running the following from Exchange Management Shell.

Of course make sure to replace @domain.com and Alerts with the required values.

$condition = Get-TransportRulePredicate HeaderContains
$condition.MessageHeader = "To"
$condition.words= @("@domain.com")

$action = Get-TransportRuleAction BlindCopyTo
$Action.Addresses = @((Get-Mailbox "Alerts"))
new-TransportRule "Rule Name" -conditions $condition -action $action

Microsoft Exchange 2007 SP1 Rollup update 9 or SP2

I’ve had the above rule running at a few customer with no issues but they stopped working once updated to SP2!
Digging up on the internet I found the following in the Exchange 2007 SP2 release notes

However, any rules that are based on the old behavior (pattern match) will not work after Update Rollup 9 or Exchange 2007 SP2 is installed. In this case, you must rebuild your transport rules to specify that a subject, body, from address, or header contains text patterns instead of specific words. For example, to obtain a pattern match rather than a word match, a transport rule that contains a “when the from address contains specific words” condition must be changed to “when the from address contains text patterns.”

OK then I had to rewrite my rule and change the HeaderContains to HeaderMatches and the result is as follows

$condition = Get-TransportRulePredicate HeaderMatches
$condition.MessageHeader = "To"
$condition.patterns = @("@domain.com")

$action = Get-TransportRuleAction BlindCopyTo
$Action.Addresses = @((Get-Mailbox "Alerts"))
new-TransportRule "Rule Name" -conditions $condition -action $action

Microsoft Exchange 2010

If you are running Exchange 2010 things are much easier, not only we have now the predicate RecipientAddressContainsWords but it is also possible to specify an email address to BCC to and not a mailbox. The transport rule is created as follows

New-TransportRule -Name "Rule Name" -RecipientAddressContainsWords '@domain.com' -BlindCopyTo '[email protected]'

In all versions of Exchange you will need to restart the Microsoft Exchange Transport service for the rule to be applied faster, you can do this by running

Restart-Service MsExchangeTransport

For the sake of example in this article I have used the action BlindCopyTo but the above can be easily modify for any other transport rule action needed. If you are uneasy with powershell you can simply create the rules as is and then go back to Exchange Management Console -> Organization Configuration -> Hub Transport -> Transport Rules and edit the action of the rule using the GUI.

I have been working in IT consultancy and solution integration since 1998 and I consider myself lucky to be, one in a few, making a living out of my passion. I am also member of the famous Experts Exchange (profile here) online community where I try my best to share what I have learned along the road.

Posted in Messaging & Collaboration Tagged with: , , , , ,
One comment on “Hub Transport Rule based on recipient’s domain
  1. Satish says:

    wow! glad I found this article…
    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*