none
Need to modify Email body using Exchange Transport Agent. RRS feed

  • Question

  • Hi,

    We need to modify email body using Exchange 2010 Transport Agent, and currently have looked into 2 online Transport Agent solutions

    1. Body Conversion Sample Application

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

    2. Bandwidth Logging Sample Application

    http://msdn.microsoft.com/en-us/library/bb204059(v=EXCHG.140).aspx

    provided by Microsoft, but still cannot understand how to do it.

    As far as I see sample, I can understand that I might need to take ReadStream, and then update email contents, and then again write the modified contents to email using WriteStream, but I have no idea, how to do it.

    I have to actually modify email message, by looking into certain Sender addresses, and then replacing a piece of text with something else.

    Can anyone help.

     


    Regards, David Johnson
    Tuesday, November 1, 2011 8:29 PM

Answers

  • Hi,

    I am now able to change the body of email using Exchange Transport agent, and I chaned the body of HMTL email using this way:

    Microsoft.Exchange.Data.Transport.Email.Body myBody; 
    System.IO.System.IO.Stream myBodyReadStream; 
    System.IO.System.IO.Stream myBodyWriteStream;
    string charsetName;
    
    //Get the body
    myBody = e.MailItem.Message.Body; 
    myBodyReadStream = myBody.GetContentReadStream(); 
    
    //Get encoding 
    charsetName = myBody.CharsetName; 
    
    //Set a new writer 
    myBodyWriteStream = myBody.GetContentWriteStream(charsetName); 
    
    // Now read stream into a byte buffer. 
    byte[] buffer = new byte[myBodyReadStream.Length]; 
    int read = 0; 
    int chunk; 
    while ((chunk = myBodyReadStream.Read(buffer, read, buffer.Length - read)) > 0)
          {
           read += chunk; 
          }
    
    //I then  converted the byte array to string
    
    // then changed the string according to my need
    
    //then converted the string back to byte array buffer
    
    
    //Write the bytearray back to the body 
    myBodyWriteStream.Write(buffer, 0, buffer.Length); 
    myBodyWriteStream.Flush();
    


    This forum post really helped me.http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/7a1dec76-014b-4989-aa52-2a3fc5676ece

    Regards,

     


    Regards, David Johnson
    • Marked as answer by Cyber Friend Friday, December 9, 2011 4:37 PM
    Friday, December 9, 2011 4:36 PM

All replies

  • Hi,

    I am now able to change the body of email using Exchange Transport agent, and I chaned the body of HMTL email using this way:

    Microsoft.Exchange.Data.Transport.Email.Body myBody; 
    System.IO.System.IO.Stream myBodyReadStream; 
    System.IO.System.IO.Stream myBodyWriteStream;
    string charsetName;
    
    //Get the body
    myBody = e.MailItem.Message.Body; 
    myBodyReadStream = myBody.GetContentReadStream(); 
    
    //Get encoding 
    charsetName = myBody.CharsetName; 
    
    //Set a new writer 
    myBodyWriteStream = myBody.GetContentWriteStream(charsetName); 
    
    // Now read stream into a byte buffer. 
    byte[] buffer = new byte[myBodyReadStream.Length]; 
    int read = 0; 
    int chunk; 
    while ((chunk = myBodyReadStream.Read(buffer, read, buffer.Length - read)) > 0)
          {
           read += chunk; 
          }
    
    //I then  converted the byte array to string
    
    // then changed the string according to my need
    
    //then converted the string back to byte array buffer
    
    
    //Write the bytearray back to the body 
    myBodyWriteStream.Write(buffer, 0, buffer.Length); 
    myBodyWriteStream.Flush();
    


    This forum post really helped me.http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/7a1dec76-014b-4989-aa52-2a3fc5676ece

    Regards,

     


    Regards, David Johnson
    • Marked as answer by Cyber Friend Friday, December 9, 2011 4:37 PM
    Friday, December 9, 2011 4:36 PM
  • I know this is a pretty old question, but I stumbled upon this question and found this blogpost really helpful:

    http://blogs.technet.com/b/postwoman/archive/2010/09/14/step-by-step-how-to-create-a-routing-agent.aspx

    This is more or less a complete solution to adding stuff to a message inside a TransportAgent.

    Be aware that you must first change the email body (e.g. inserting the <img src="..." /> and later adding the images as attachments, otherwise "inline" attachments will always be treated like normal attachments - which is a really strange behavior in my eyes and costs me countless hours.

    Wednesday, February 17, 2016 1:11 PM
  • I know It's been a long time since this post started, but I need those Code Samples (linked in the original post) and you probably noticed that Microsoft has removed them! Any help will be appreciated.

    Thanks


    If you believe you can, you might. If you know you can, you will

    Sunday, July 26, 2020 2:38 PM
  • Hi,

    This link from the above post also contains good examples of the source code, and explanation.

    https://docs.microsoft.com/en-us/archive/blogs/postwoman/step-by-step-how-to-create-a-routing-agent

    Thanks


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com

    Sunday, July 26, 2020 10:32 PM