I have an email message in a MailMessage object. and I want to be able to represent it as an XPS file. This particular email has a small body (15 lines of text or so) and several much larger pdf attachments.
In order to be create the XPS file you suggested I follow these steps:
- Construct a MemoryStream object
- Save the MailMessage content in MHTML format to the MemoryStream
- Use the MemoryStream to construct an Aspose.Words.Document
- Use the Document Save method to create the XPS file.
A problem I have is that MemoryStream intermittently throws an exception when it tries to resize its internal buffer. Here's a stack dump. My SaveAsXps method has just made a call to save the MailMessage as MHTML format:
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.IO.MemoryStream.set_Capacity(Int32 value) at System.IO.MemoryStream.EnsureCapacity(Int32 value) at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at .Write(Byte[] , Int32 , Int32 ) at .Write(Byte[] , Int32 , Int32 ) at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) at System.IO.StreamWriter.Write(String value) at .Write(Byte[] , Int32 , Int32 ) at . ( , Boolean ) at . ( , Boolean ) at . ( ) at Aspose.Email.Mail.MailMessage.( ) at Aspose.Email.Mail.MailMessage.Save(Stream stream, SaveOptions options) at Aspose.Email.Mail.MailMessage.Save(Stream stream) at Aspose.Email.Mail.MailMessage.Save(Stream stream, SaveOptions options) at Kutana.Kappris.Manipulation.MsgHandler.SaveAsXps(CT_FileNode parent) at Kutana.Kappris.Manipulation.PrintFu.SaveAsXps(CT_FileNode parent)
I don't have a way of reproducing this error at will - it is intermittent and seems to depend somewhat on system load and allocations made by other code in my app. It is not dependent on msg content.
Q1 - Have you seen this exception? How can I avoid it?
Q2 - the saved MemoryStream contains everything that's in the MailMessage - all the attachments, and all the message headers and content. But in order to create an XPS file of the email body text, the MemoryStream doesn't need all the attachments - it only really needs the inline attachments. Is there any way of filling a MemoryStream with MHTML content so that the non-inline attachments are omitted? Then the MemoryStream would not need to have such a large internal buffer and wouldn't need to be resized so often. In particular, the msg I am trying to save as xps is about 4MB because of all its attachments, but its body content is only about 10KB - a big difference!