none
Certain characters are getting with special characters via custom transport agent-Exchange Server 2010 RRS feed

  • Question

  • Hi,

    I have created custom transport agent to replace certain values in the email body.

    It was working fine,after there were certain security updates installed on the production server.

    After that,certain characters are getting replaced with special characters which is annoying.

    I have changed the code from ASCIIEncoding enc = new ASCIIEncoding() to UTF8Encoding enc = new UTF8Encoding()

    But still issue persists,please kindly share your ideas/thoughts to correct this production issue.

    Complete 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 + ContentID.Length + 16];
                        Byte[] newbodyarray = new Byte[mailbodyarray.Length+16];                    

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

                        if (str.Contains("abd"))
                        {
                            str = str.Replace("abd", "xyz");
                        }          


                        //newbodyarray = enc.GetBytes(str);
                        //newbodyarray = enc.GetBytes(strMaskEmailAddress);

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

                    }

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


        }
    }

    Regards,

    Sudheer


    Thanks & Regards, Sudheer

    Saturday, November 3, 2018 4:49 AM

All replies

  • Email can be encoded many different ways so generally you should be reading the encoding first and then use that to read/write the Body-stream eg

                                charsetName = body.CharsetName;
                                if (null == charsetName ||
                                    !Microsoft.Exchange.Data.Globalization.Charset.TryGetEncoding(charsetName, out encoding))
                                {
                                    // Either no charset, or charset is not supported by the system.
                                    return;
                                }
    Cheers
    Glen


    Sunday, November 4, 2018 11:02 PM