Forum Discussion

IanaMac's avatar
IanaMac
Brass Contributor
Sep 20, 2023
Solved

Limit the number domains in recipient list on mail

Hi all

We have an issue where one of our support teams using a shared mailbox were a user inadvertently sent an e mail to an incorrect client due to preemptive  listing of e mail address's

I have been tasked with finding a solution where users can only add 1 domain plus our internal domains.

 

I would prefer to find a solution that resides on the exchange side rather than the client side, i am sure that we are not the only organisation that has this requirement!!! 

 

Many thanks for any suggestions 

  • Hi IanaMac,

    yes, you need to add domains manually if you are using GUI, but you can add domains automatically to the list using a PowerShell script to read the list of domains from a file and then add them to the list of blocked domains in the transport rule or mail flow rule.

    Here is an example of a PowerShell script to add domains automatically to the list of blocked domains in a transport rule from the .txt. file:

     

    # Import the Exchange module
    Import-Module Exchange
    
    # Get the transport rule
    $transportRule = Get-TransportRule "Block messages to external domains"
    
    # Add the domains to the list of blocked domains
    foreach ($domain in Get-Content "Domains.txt") {
        $transportRule.Conditions.RecipientDomain.BlockedDomains.Add($domain)
    }
    
    # Set the transport rule
    Set-TransportRule $transportRule

     


    To use this script, you would first need to create a text file called Domains.txt that contains the list of domains that you want to block.

    - Once you have created the text file, you can run the PowerShell script to add the domains to the list of blocked domains in the transport rule.

     

    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.


    If the post was useful in other ways, please consider giving it Like.


    Kindest regards,


    Leon Pavesic

7 Replies

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi IanaMac,

    There are a couple of ways to limit the number of domains in the recipient list on mail in Exchange:

    • Use a mail flow rule. A mail flow rule is a rule that can be applied to all outgoing messages in your Exchange organization. To create a mail flow rule to limit the number of domains in the recipient list, follow these steps:

      1. Open the Exchange admin center.
      2. Go to Mail flow > Rules.
      3. Click New rule.
      4. Give the rule a name and description.
      5. Under Apply this rule if, select the Any message condition.
      6. Under Do the following, select the Block the message action.
      7. Click Add condition.
      8. Under Select a condition, select the Recipient domain condition.
      9. Under Configure the condition, select the Matches any of the following patterns option and enter the following patterns:
    *.example.com
    *.example.net
    1. Click OK.
    2. Click Save.
    • Use a transport rule. A transport rule is similar to a mail flow rule, but it is processed before mail flow rules. To create a transport rule to limit the number of domains in the recipient list, follow these steps:

      1. Open the Exchange admin center.
      2. Go to Organization > Transport rules.
      3. Click New rule.
      4. Give the rule a name and description.
      5. Under Apply this rule if, select the Any message condition.
      6. Under Do the following, select the Reject the message action.
      7. Click Add condition.
      8. Under Select a condition, select the Recipient domain condition.
      9. Under Configure the condition, select the Matches any of the following patterns option and enter the following patterns:
    *.example.com
    *.example.net
    1. Click OK.
    2. Click Save.

    Here are some additional links to information about limiting the number of domains in the recipient list on mail in Exchange:

    • Victor_Ivanidze's avatar
      Victor_Ivanidze
      Bronze Contributor

      Hi LeonPavesic,

      could you please tell me why I don't see "Transport rules" under "Organization"? 

      There is only one subitem under "Organization": "Sharing".

      Thanks.

      • LeonPavesic's avatar
        LeonPavesic
        Silver Contributor

        Hi Victor_Ivanidze,

        thanks for your update. you are right, I wrote it wrong.

        You can find Transport rules under Mail flow --> Rules. 

        (and not under Organization)

         

        Please click Mark as Best Response & Like if my post helped you to solve your issue.
        This will help others to find the correct solution easily. It also closes the item.


        If the post was useful in other ways, please consider giving it Like.


        Kindest regards,


        Leon Pavesic
        (LinkedIn)

    • IanaMac's avatar
      IanaMac
      Brass Contributor
      Hi Leon
      thanks for this the transport and mailflow rules mean that we would have to add 100 of domains to the list...
      I will look at the documentation now
      • LeonPavesic's avatar
        LeonPavesic
        Silver Contributor

        Hi IanaMac,

        yes, you need to add domains manually if you are using GUI, but you can add domains automatically to the list using a PowerShell script to read the list of domains from a file and then add them to the list of blocked domains in the transport rule or mail flow rule.

        Here is an example of a PowerShell script to add domains automatically to the list of blocked domains in a transport rule from the .txt. file:

         

        # Import the Exchange module
        Import-Module Exchange
        
        # Get the transport rule
        $transportRule = Get-TransportRule "Block messages to external domains"
        
        # Add the domains to the list of blocked domains
        foreach ($domain in Get-Content "Domains.txt") {
            $transportRule.Conditions.RecipientDomain.BlockedDomains.Add($domain)
        }
        
        # Set the transport rule
        Set-TransportRule $transportRule

         


        To use this script, you would first need to create a text file called Domains.txt that contains the list of domains that you want to block.

        - Once you have created the text file, you can run the PowerShell script to add the domains to the list of blocked domains in the transport rule.

         

        Please click Mark as Best Response & Like if my post helped you to solve your issue.
        This will help others to find the correct solution easily. It also closes the item.


        If the post was useful in other ways, please consider giving it Like.


        Kindest regards,


        Leon Pavesic

Resources