none
Build a custom transport agent to deploy in the exchange 2010 server for outbound emails RRS feed

  • Question

  • Hi,

    I am supposed to build a transport agent which should replace or rewrite or mask all the email addresses 

    in the message body thread with @abc.com [for example]

    Please share your ideas/inputs to achieve.

    Regards,

    Sudheer


    Thanks & Regards, Sudheer

    Monday, October 1, 2018 6:46 AM

All replies

  • Hi Sudheer,

    According to my research, I found  the following sample to build a transport agent.

    Custom Transport Agent for Exchange Server 2010

    Hopefully it helps you.

    Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, October 1, 2018 7:25 AM
  • Hi Sudheer,

    Did you resolve your problem now? Any updates for this? if your issue is still exist then let us know about that, we will try to suggest you further to solve the issue. 

    Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, October 8, 2018 4:49 AM
  • Hi,

    I wrote the below code for the expectation,hope this works and I haven't tested yet

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;

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

    using Microsoft.Exchange.Data.Mime;

    using System.IO;
    using System.Net;

    namespace UpFieldMailBodyMask
    {
        public sealed class AgentFactory : RoutingAgentFactory
        {
            public override RoutingAgent CreateAgent(SmtpServer server)
            {
                return new AgentClass();
            }
        }
        public class AgentClass : RoutingAgent
        {
            public AgentClass()
            {
                base.OnRoutedMessage += AgentClass_OnRoutedMessage;
            }

            private void AgentClass_OnRoutedMessage(RoutedMessageEventSource source, QueuedMessageEventArgs e)
            {
                //throw new NotImplementedException();
                MaskEmailAddresses(e.MailItem.Message);
            }

            private void MaskEmailAddresses(EmailMessage emailMessage)
            {
                //throw new NotImplementedException();
                try
                {
                    Body body = emailMessage.Body;
                    if (body.BodyFormat == BodyFormat.Html)
                    {
                        Stream origBody;
                        Stream newBody;
                        if (!body.TryGetContentReadStream(out origBody))
                            throw new InvalidDataException("Could not read html body.");

                        Byte[] mailbodyarray = new Byte[origBody.Length];
                        origBody.Read(mailbodyarray, 0, (int)origBody.Length);
                        origBody.Close();

                        //Byte[] newbodyarray = new Byte[mailbodyarray.Length + ContentID.Length + 16];
                        Byte[] newbodyarray = new Byte[mailbodyarray.Length];

                        //String regex = @"(.{2}).+@.+(.{2}(?:\..{2,3}){1,2})";
                        //String replace = "$1*@*$2";
                        //Regex.Replace(mailbodyarray.ToString(), regex, replace);

                        ASCIIEncoding enc = new ASCIIEncoding();
                        String str = enc.GetString(mailbodyarray);
                        int i = str.LastIndexOf("</body>");

                        //int index = str.IndexOf('@');
                        //string returnValue = str.Replace(str.Substring(index - 3, 3), "***").Replace(str.Substring(index + 1, 3), "***");

                        //str = str.Replace("@", "***");          

                        
                        string regex = @"[\w-\._\+%]{2}@[\w-\._\+%]{2}";
                        string strMaskEmailAddress = Regex.Replace(str, regex, "**@**");

                        //newbodyarray = enc.GetBytes(str);
                        newbodyarray = enc.GetBytes(strMaskEmailAddress);
                        newBody = body.GetContentWriteStream();
                        newBody.Write(newbodyarray, 0, newbodyarray.Length);
                        newBody.Flush();
                        newBody.Close();

                    }
                }
                catch (InvalidOperationException ioex)
                {

                }

                
            }

            
        }
    }

    Regards,

    Sudheer


    Thanks & Regards, Sudheer

    Monday, October 8, 2018 6:20 AM
  • Hi Sudheer,

    Thanks for your reply. You can try it if you have any time. if worked then try to update the status of the thread then mark it as an answer. Thanks for your understanding.

    Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, October 8, 2018 6:24 AM
  • Hi Sudheer,

    This thread has been around for a long time. Please remember to mark the replies as answers if they helped. Please help us close the thread. Thanks for your understanding.

    Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, October 16, 2018 4:37 PM
  • Hi,

    The above code is replacing the text with ASCIIEncoding enc = new ASCIIEncoding(); but certain characters are getting replaced as ?????

    The characters are getting rewritten if it is changed to UnicodeEncoding enc = new UnicodeEncoding(); but string is not getting replaced through below condition

    Please let me know if I am missing anything as this is a production issue.

    if (str.Contains("upfield.online"))
                        {
                            str = str.Replace("abc.online", "def.com");
                        }

    Complete source code

                                                                                                      

    using System;
    using System.Collections.Generic;

    using System.Text;
    using System.Text.RegularExpressions;

    using System.Diagnostics;

    //Exchange Server 2010 DLLs
    using Microsoft.Exchange.Data.Transport;
    using Microsoft.Exchange.Data.Transport.Email;
    using Microsoft.Exchange.Data.Transport.Routing;

    using Microsoft.Exchange.Data.Mime;

    using System.IO;
    using System.Net;

    namespace UpFieldMailBodyMask
    {
        public sealed class AgentFactory : RoutingAgentFactory
        {
            public override RoutingAgent CreateAgent(SmtpServer server)
            {
                return new AgentClass();
            }
        }
        public class AgentClass : RoutingAgent
        {
            public AgentClass()
            {
                base.OnRoutedMessage += AgentClass_OnRoutedMessage;           

            }        

            private void AgentClass_OnRoutedMessage(RoutedMessageEventSource source, QueuedMessageEventArgs e)
            {
                //throw new NotImplementedException();
                MaskEmailAddresses(e.MailItem.Message);
            }

            /// <summary>
            /// Method to Replace domain name
            /// </summary>
            /// <param name="emailMessage"></param>
            private void MaskEmailAddresses(EmailMessage emailMessage)
            {
                //throw new NotImplementedException();
                try
                {

                    Body body = emailMessage.Body;
                    if (body.BodyFormat == BodyFormat.Html)
                    {
                        Stream origBody;
                        Stream newBody;
                        if (!body.TryGetContentReadStream(out origBody))
                            throw new InvalidDataException("Could not read html body.");

                        Byte[] mailbodyarray = new Byte[origBody.Length];
                        origBody.Read(mailbodyarray, 0, (int)origBody.Length);
                        origBody.Close();


                        Byte[] newbodyarray = new Byte[mailbodyarray.Length+16];

                        //ASCIIEncoding enc = new ASCIIEncoding();
                        UnicodeEncoding enc = new UnicodeEncoding();
                        String str = enc.GetString(mailbodyarray);
                        int i = str.LastIndexOf("</body>");                   

                        if (str.Contains("abc.online"))
                        {
                            str = str.Replace("abc.online", "def.com");
                        }       

                        newbodyarray = enc.GetBytes(str);
                        newBody = body.GetContentWriteStream();
                        newBody.Write(newbodyarray, 0, newbodyarray.Length);
                        newBody.Flush();
                        newBody.Close();                   

                    }

                }
                catch (InvalidOperationException ioex)
                {
                    EventLog.WriteEntry("Mail Body Mask Exception Create", ioex.Message + "Trace" + ioex.StackTrace, EventLogEntryType.Error, 121, short.MaxValue);
                }            
            }       


        }
    }

    Regards,

    Sudheer



    Thanks & Regards, Sudheer

    Tuesday, November 6, 2018 10:19 AM