none
e.MailItem.Message.MimeDocument.RootPart.Headers.AppendChild(sizeHeader) throwing Exception: Sample Transport Agent

    Question

  • Hi,

    I m building a custom Transport Agent on HUB Server and using this piece of code from Sample Transport Agent "BandwidthLogging":

     private void SubmittedMessage(
                SubmittedMessageEventSource source,
                QueuedMessageEventArgs e)
            {
                Debug.WriteLine(logPrefix + "Message submitted");
    
                if (true)
                {
                    Header sizeHeader = Header.Create("CustomHeader");
    
                    sizeHeader.Value = e.MailItem.MimeStreamLength.ToString();
                    //This line is causing problem:  
                    e.MailItem.Message.MimeDocument.RootPart.Headers.AppendChild(sizeHeader);
                }
            }
    


    , which is throwing runtime exception: MimeNode.AppendChild was called on a read-only MIME document.

    Full Exception with Stack Trace:

    The transport process failed during message processing with the following call stack: Microsoft.Exchange.Data.Mime.ReadOnlyMimeException: MimeNode.AppendChild was called on a read-only MIME document.

       at Microsoft.Exchange.Data.Mime.MimeNode.ThrowIfReadOnly(String method)

       at Microsoft.Exchange.Data.Mime.MimeNode.AppendChild(MimeNode newChild)

       at Microsoft.Exchange.Samples.Agents.HubTransportLogging.MessageLogger.OnSubmittedMessageHandler(SubmittedMessageEventSource source, QueuedMessageEventArgs e) in C:\Samples\HubSynchronousLogging\HubTransportLogging.cs:line 126

       at Microsoft.Exchange.Data.Transport.Routing.RoutingAgent.Invoke(String eventTopic, Object source, Object e)

       at Microsoft.Exchange.Data.Transport.Internal.MExRuntime.Dispatcher.Invoke(MExSession session)

       at Microsoft.Exchange.Data.Transport.Internal.MExRuntime.MExSession.AsyncInvoke(Object state)

       at Microsoft.Exchange.Data.Transport.Internal.MExRuntime.MExSession.BeginInvoke(String topic, Object source, Object e, AsyncCallback callback, Object callbackState)

       at Microsoft.Exchange.Transport.Categorizer.MExEvents.RaiseEvent(MExSession mexSession, String eventTopic, AsyncCallback callback, Object state, Object[] contexts)

       at Microsoft.Exchange.Transport.Categorizer.MExEvents.RaiseOnSubmittedMessage(TaskContext context, AsyncCallback callback, MailItem mailItem)

       at Microsoft.Exchange.Transport.Categorizer.CategorizerComponent.Stage1OnSubmitted(TransportMailItem transportMailItem, TaskContext taskContext)

       at Microsoft.Exchange.Transport.Categorizer.TaskContext.Invoke()

       at Microsoft.Exchange.Transport.Categorizer.CatScheduler.JobThreadEntry(Object ignored)

       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)

       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)

     

    Exception belongs to this line of  code:

    e.MailItem.Message.MimeDocument.RootPart.Headers.AppendChild(sizeHeader);
    

    Any idea on this?

    Regards,


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
    Friday, September 16, 2011 10:26 PM

Answers

  • Hi,

    For reference of others, I used these lines of code to add a custom header to email header.                   

    MimeDocument mdMimeDoc = e.MailItem.Message.MimeDocument; 
    
    HeaderList hlHeaderlist = mdMimeDoc.RootPart.Headers;
    
    MimeNode lhLasterHeader = hlHeaderlist.FirstChild;
    
    Header header = hlHeaderlist.FindFirst(sizeHeaderName);
    
    if (header == null)
    {                        
       //custom header
       TextHeader nhNewHeader = new TextHeader(sizeHeaderName, szHeaderValue);
       hlHeaderlist.InsertBefore(nhNewHeader, lhLasterHeader);                    
    }

    Regards,


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

    • Marked as answer by Laeeq Qazi Friday, March 02, 2012 2:32 PM
    Friday, March 02, 2012 2:32 PM

All replies

  • you can't modify the mimedocument within the message. the only way to do this is to create a mimeDocument object, take the mime parts and construct the document, edit it with the parts you want and then create a new EmailMessage with the MimeDocument overloaded constructor. then you can send that message and delete the old one.
    Thursday, February 16, 2012 6:19 AM
  • you can't modify the mimedocument within the message. the only way to do this is to create a mimeDocument object, take the mime parts and construct the document, edit it with the parts you want and then create a new EmailMessage with the MimeDocument overloaded constructor. then you can send that message and delete the old one.

    Yes you are right, and it was obvious that problem is in code. But the problem is, why Microsfot has released this buggly sample, which doesnt work properly.

    Finally I utilized some other way to get the solution, which I needed, above sample transport agent never ran successfully.

    Regards,


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

    Friday, February 17, 2012 11:47 PM
  • Hi,

    For reference of others, I used these lines of code to add a custom header to email header.                   

    MimeDocument mdMimeDoc = e.MailItem.Message.MimeDocument; 
    
    HeaderList hlHeaderlist = mdMimeDoc.RootPart.Headers;
    
    MimeNode lhLasterHeader = hlHeaderlist.FirstChild;
    
    Header header = hlHeaderlist.FindFirst(sizeHeaderName);
    
    if (header == null)
    {                        
       //custom header
       TextHeader nhNewHeader = new TextHeader(sizeHeaderName, szHeaderValue);
       hlHeaderlist.InsertBefore(nhNewHeader, lhLasterHeader);                    
    }

    Regards,


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

    • Marked as answer by Laeeq Qazi Friday, March 02, 2012 2:32 PM
    Friday, March 02, 2012 2:32 PM