Get body message exchange server in UTF-8 through transport agent

Hello

I'm trying to save the body message of transport mail item.

Below is my code:

public class MyRoutingAgentFactory : RoutingAgentFactory 
    { 
        public override RoutingAgent CreateAgent(SmtpServer server) 
        { 
            return new MyRoutingAgent(); 
        } 
    }   
    public class MyRoutingAgent : RoutingAgent 
    { 
        public MyRoutingAgent() 
        { 
            base.OnSubmittedMessage += new SubmittedMessageEventHandler(MyRoutingAgent_OnSubmittedMessage); 
        }   
        void MyRoutingAgent_OnSubmittedMessage(SubmittedMessageEventSource source, QueuedMessageEventArgs e)
        {
            string charsetName;
            charsetName = e.MailItem.Message.Body.CharsetName;
            StreamReader reader = new StreamReader(e.MailItem.Message.Body.GetContentReadStream(), Encoding.Unicode, true);
            FileStream fso = new FileStream(@"C:\body.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter writer = new StreamWriter(fso, Encoding.UTF8);
            string temp;
            while ((temp = reader.ReadLine()) != null)
            {
                writer.WriteLine(temp);
            }
            writer.WriteLine(e.MailItem.Message.Body.ToString());
            writer.Flush();
            writer.Close();

            fso.Close();
            reader.Close();

}

}

I sent a test email to get the content of message

Body message that I sent is "Hello, this is test"

But the message I catch in the file not show the content "Hello, this is test". 

Body of message is

搼㰾⼼㰾㰾搼搼搼㰾

So what's problem with my encoding ?

How can I fix it. 

July 23rd, 2013 1:57pm

I would suggest you take a look at the Body conversion sample from the SDK which has an example of how to access the Body depending on what type of message your dealing with http://msdn.microsoft.com/en-us/library/exchange/bb204066(v=exchg.140).aspx. I'd say what your seeing in the TNEF content because the message is TNEF format see http://blogs.msdn.com/b/mstehle/archive/2009/01/13/howto-sample-transport-agent-add-headers-categories-mapi-props-even-uses-a-fork.aspx for more details.

Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2013 5:06am

I was interested in that first link, but there's an extra dot on the end that makes it look like msdn has crashed when you click on it :)  Here's the same link without the extra dot:

http://msdn.microsoft.com/en-us/library/exchange/bb204066(v=exchg.140).aspx

July 24th, 2013 6:31am

I detect the encoding type of mailitem and convert to utf8 encoding. 

This way give me the expectation result.

Thank you for helping me.

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2013 3:44pm

I was interested in that first link, but there's an extra dot on the end that makes it look like msdn has crashed when you click on it :)  Here's the same link without the extra dot:

msdn.microsoft.com/en-us/library/exchange/bb204066(v=exchg.140).aspx

Yes, this link works well

July 24th, 2013 10:10pm

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

Other recent topics Other recent topics