Cannot Access a closed Stream

MailMessage message = Aspose.Email.Mail.MailMessage.Load(fileName);
using (MemoryStream stream = new System.IO.MemoryStream())
{
message.Save(stream, MailMessageSaveType.MHtmlFromat);


LoadOptions loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = LoadFormat.Mhtml;

Document document = new Aspose.Words.Document(stream, loadOptions);
Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Html);
document.Save(“C:\temp\output.html”, saveOptions);

}


I am using the above code to convert a .msg file to html. As I’ve reviewed in other posts I must first convert the .msg to a MHtml file and then convert to an Html.

However when the code runs the bolded line, I get an exception “Cannot access a closed stream.” I am not closing the memory stream, so I am not sure why it is closed.

Any ideaS?

Hi Vennila,


Thank you for contacting support.

We have already reported and fixed your said problem with some previous version of Aspose.Email and Aspose.Words for .NET components. I would request you to please download the latest version of both products from the download section and execute the same code on your end. I hope you will not experience the problem any more. In case your problem persists, please provide us your sample message for our review.

Regards,

I have installed the latest files as of may 31 , 2012

Is there a sample project I can download that uses this code

Hi Vennila,


Adding to Babar’s comments please send some sample MSG file which is causing this issue. It will help us to analyze the issue and provide solution for this issue.

Best Regards

I’ve attached the file

Please rename it from a .zip to a .msg

(your system does not allow uploading of .msg files)

Thanks

Hi Vennila,

Thank you very much for providing this MSG file.

I am afraid to inform that I have tested the code and found no bug with the given MSG file. This MSG file is successfully converted to html and opened properly in web browser.

I have made a slight change in the code as given below:
MailMessage message = Aspose.Email.Mail.MailMessage.Load("MsgToHtml.msg");
using (MemoryStream stream = new System.IO.MemoryStream())
{
message.Save(stream, MailMessageSaveType.MHtmlFromat);
Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = LoadFormat.Mhtml;
Document document = new Aspose.Words.Document(stream, loadOptions);
Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Html);
document.Save("output.html", saveOptions);
}
I am attaching the output file for your reference.

Please check this code using Aspose.Email for .NET 1.8.0 and Aspose.Words for .NET 11.3.0.

Best Regards

I needed to take it one step further and return a string representation of the html. However by doing this, I had to set a location for the images to be copied to within the msg file. But now my problem is when I want to view the html in Silverlight. Silverlight does not have access to the file system directly.

MailMessage message = Aspose.Email.Mail.MailMessage.Load(fileName);

using (MemoryStream stream = new System.IO.MemoryStream())
{

message.Save(stream, MailMessageSaveType.MHtmlFromat);

[//stream.Flush](https://stream.flush/)();
[//stream.Position](https://stream.position/) = 0;

LoadOptions loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = LoadFormat.Mhtml;
MemoryStream s2 = new MemoryStream();

byte[] t1 = stream.ToArray();
s2.Write(t1, 0, t1.Length);

Document document = new Aspose.Words.Document(s2, loadOptions);
Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Html);
saveOptions.ImagesFolder = “C:\temp”;
MemoryStream convertedStream = new MemoryStream();
document.Save(convertedStream, saveOptions);

returnValue = Encoding.UTF8.GetString(convertedStream.ToArray());

}

Hi Vennila,

Thank you very much for your continuous feedback.

In the current scenario, Aspose.Email for .NET is used to convert MSG file to Mhtml format, which is working fine. Next Aspose.Words for .NET is used to convert Mhtml to Html which is also working fine.

Now regarding next issue of denial of access to local file system for Silverlight, I would like to share that Silverlight version 4 and below don’t allow access to local file system. However upward versions provide this access under certain conditions like the application which is accessing the local file system is trusted application and also accessed folders are from few special folders like MyDocuments, Pictures etc.

I am afraid to inform that we have no further details of working of Silverlight. You may please consult relevant forums to find out solution to this problem. Please refer to the following link for your reference:
http://www.silverlight.net/learn/graphics/file-and-local-data/local-file-access.

Feel free to write us if any issue is there related to usage of Aspose family of products.

Best Regards