Custom routing agent based on sender's security group and subject

I made a custom routing agent that routes mails contains the word [encrypt] in the subject and sent from domain test.com

The part of the code is

if (e.MailItem.FromAddress.DomainPart.Contains("test.com")
                && e.MailItem.Message.Subject.Contains("[encrypt]"))

now what i need is to route mails based on the membership of a certain security group like "securemail" not the whole domain. ie if the sender is a member in security group (securemail) and the subject contains the word [encrypt] route the mail

Thanks

April 8th, 2015 3:24am

I made a custom routing agent that routes mails contains the word [encrypt] in the subject and sent from domain test.com

These mails are routed through a new send connector that has a smart host

The part of the code is

if (e.MailItem.FromAddress.DomainPart.Contains("test.com")
                && e.MailItem.Message.Subject.Contains("[encrypt]"))

now what i need is to route mails based on the membership of a certain security group like "securemail" not the whole domain. ie if the sender is a member in security group (securemail) and the subject contains the word [encrypt] route the mail

Here is the link that i used to compile the agent

http://blogs.technet.com/b/appssrv/archive/2009/08/26/how-to-control-routing-from-your-own-routing-agent.aspx#pi47623=1

Thanks

Free Windows Admin Tool Kit Click here and download it now
April 9th, 2015 12:43pm

I made a custom routing agent that routes mails contains the word [encrypt] in the subject and sent from domain test.com

These mails are routed through a new send connector that has a smart host

The part of the code is

if (e.MailItem.FromAddress.DomainPart.Contains("test.com")
                && e.MailItem.Message.Subject.Contains("[encrypt]"))

now what i need is to route mails based on the membership of a certain security group like "securemail" not the whole domain. ie if the sender is a member in security group (securemail) and the subject contains the word [encrypt] route the mail

Here is the link that i used to compile the agent

http://blogs.technet.com/b/appssrv/archive/2009/08/26/how-to-control-routing-from-your-own-routing-agent.aspx#pi47623=1

Thanks

April 9th, 2015 4:42pm

I made a custom routing agent that routes mails contains the word [encrypt] in the subject and sent from domain test.com

These mails are routed through a new send connector that has a smart host

The part of the code is

if (e.MailItem.FromAddress.DomainPart.Contains("test.com")
                && e.MailItem.Message.Subject.Contains("[encrypt]"))

now what i need is to route mails based on the membership of a certain security group like "securemail" not the whole domain. ie if the sender is a member in security group (securemail) and the subject contains the word [encrypt] route the mail

Here is the link that i used to compile the agent

http://blogs.technet.com/b/appssrv/archive/2009/08/26/how-to-control-routing-from-your-own-routing-agent.aspx#pi47623=1

Thanks

Free Windows Admin Tool Kit Click here and download it now
April 9th, 2015 4:42pm

The only way to find if a user is a member of a group is to make a query into Active Directory using LDAP etc, generally for performance and reliability reasons doing that for every email that your transport agent is going to process isn't a good idea. You would be better to cache that user list so it can be easily access from within your transport agent then build something to maintain the cache.

Cheers
Glen

April 9th, 2015 10:15pm

I am very ignorant in writing codes, but can't the above code be edited to include the name of security group rather than the whole domain. i tried reading about mailitem.fromaddress property from the above link but couldn't get much as I'm not a developer

https://msdn.microsoft.com/en-us/library/microsoft.exchange.data.transport.mailitem.fromaddress(v=exchg.150).aspx

Free Windows Admin Tool Kit Click here and download it now
April 10th, 2015 5:40pm

What version of Exchange are you using ? actually I was wrong from 2010 you can now use the Address Book class in a Transport agent to check for group membership. Have a look at https://msdn.microsoft.com/en-us/library/microsoft.exchange.data.transport.addressbook.ismemberof(v=exchg.150).aspx that should allow you to check the membership of the From address (it won't work in 2007). If you can't get a piece of code to work post the code your trying.

Cheers
Glen

April 10th, 2015 8:03pm

Thanks for your answer Glen

The following  code is on exchange 2010 but i need it to check for a security group membership if possible

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

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 RoutingAgentOverride
{
    public class SampleRoutingAgentFactory : RoutingAgentFactory
    {
        public override RoutingAgent CreateAgent(SmtpServer server)
        {
            RoutingAgent myAgent = new ownRoutingAgent();

            return myAgent;
        }
    }
}
public class ownRoutingAgent : RoutingAgent
{
    public ownRoutingAgent()
    {
        //subscribe to different events
        base.OnResolvedMessage += new ResolvedMessageEventHandler(ownRoutingAgent_OnResolvedMessage);
    }

    void ownRoutingAgent_OnResolvedMessage(ResolvedMessageEventSource source, QueuedMessageEventArgs e)
    {
        try
        {
            // For testing purposes we do not only check the sender address but the subject line as well
            // If the subject contains the substring "REDIR" then the default routing is overwritten.
            //
            // Instead of hard-coding the sender you could also perform an LDAP-query, read the information
            // from a text file, etc.
            //
            if (e.MailItem.FromAddress.DomainPart.Contains("contoso.com")
                && e.MailItem.Message.Subject.Contains("[encrypt]"))
            {

                // Here we set the address space we want to use for the next hop. Note that this doesn't change the recipient address.
                // Setting the routing domain to "nexthopdomain.com" only means that the routing engine chooses a suitable connector
                // for nexthopdomain.com instead of using the recpient's domain.

                RoutingDomain myRoutingOverride = new RoutingDomain("nexthopdomain.com");

                foreach (EnvelopeRecipient recp in e.MailItem.Recipients)
                {
                    recp.SetRoutingOverride(myRoutingOverride);

                }

            }
        }

        catch // (Exception except)
        {

        }
    }

}

Free Windows Admin Tool Kit Click here and download it now
April 11th, 2015 9:39am

You need to pass in an instance of the SMTPServer object when you create the class and then you can just use the isMemberOf Method of the AddressBook interface eg

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

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 RoutingAgentOverride
{
    
    public class SampleRoutingAgentFactory : RoutingAgentFactory
    {
        
        public override RoutingAgent CreateAgent(SmtpServer server)
        {
            RoutingAgent myAgent = new ownRoutingAgent(server);           
            return myAgent;
        }
    }
}
public class ownRoutingAgent : RoutingAgent
{
    private SmtpServer _smtpServer;
    public ownRoutingAgent(SmtpServer server)
    {
        //subscribe to different events
        base.OnResolvedMessage += new ResolvedMessageEventHandler(ownRoutingAgent_OnResolvedMessage);
        _smtpServer = server;

    }

    void ownRoutingAgent_OnResolvedMessage(ResolvedMessageEventSource source, QueuedMessageEventArgs e)
    {
        try
        {
            // For testing purposes we do not only check the sender address but the subject line as well
            // If the subject contains the substring "REDIR" then the default routing is overwritten.
            //
            // Instead of hard-coding the sender you could also perform an LDAP-query, read the information
            // from a text file, etc.
            //
            if (e.MailItem.FromAddress.DomainPart.Contains("contoso.com")
                && e.MailItem.Message.Subject.Contains("[encrypt]"))
            {
                if (_smtpServer.AddressBook.IsMemberOf(e.MailItem.FromAddress, new RoutingAddress("grouptocehck@domain.com")))
                {

                    // Here we set the address space we want to use for the next hop. Note that this doesn't change the recipient address.
                    // Setting the routing domain to "nexthopdomain.com" only means that the routing engine chooses a suitable connector
                    // for nexthopdomain.com instead of using the recpient's domain.

                    RoutingDomain myRoutingOverride = new RoutingDomain("nexthopdomain.com");

                    foreach (EnvelopeRecipient recp in e.MailItem.Recipients)
                    {
                        recp.SetRoutingOverride(myRoutingOverride);

                    }
                }

            }
        }

        catch // (Exception except)
        {

        }
    }

}
Cheers
Glen

April 13th, 2015 12:02am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics