Problems converting MSG files to PDF

Hi,

While converting MSG files to PDF I have noticed that the results are rather unpredictable.

I have attached 2 MSG files (email1.msg and email2.msg).

When converting email1.msg to PDF, an exception of type PleaseReportException is thrown.

When converting email2.msg, no exception is thrown, but the output is a blank page.

I’m using Aspose.Pdf 3.8.0, Aspose.Network 4.5.0 and Aspose.Words 5.3.0.

Below is the code I’m using to perform the conversion:

MapiMessage message = MapiMessage.FromStream(fileStream);

using(MemoryStream tempStream = new MemoryStream())
{
    TextWriter textWriter = new StreamWriter(tempStream);
    textWriter.Write(message.BodyRtf);

    Aspose.Words.Document doc = new Aspose.Words.Document(tempStream, null, LoadFormat.Rtf, null);

    using(MemoryStream stream = new MemoryStream())
    {

        doc.Save(stream, SaveFormat.AsposePdf);
        stream.Seek(0, SeekOrigin.Begin);

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(stream);

        Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
        pdf.BindXML(xmlDoc, null);

        array = pdf.GetBuffer();
    }

    textWriter.Close();
}

Hello Joao,
Thanks for considering Aspose.
Can you please save the mail contents into word document and share the files. In fact when you are calling the method to save word document contents into Intermediate XML having Aspose.Pdf format, you can use the same method to generate a Word file. This approach will help us in understanding the fact that either the email contents are properly being exported into Word file or the issue is occurring at Aspose.Pdf end, when generating the PDF file.
You can also refer to the following link for information on Saving Documents.

Hi,

Yeah, the problems seems to happen when converting the emails to Word.

When trying to convert the file “email1.msg”, an exception is thrown when reaching the following line:

Document doc = new Document(memoryStream, null, LoadFormat.Rtf, null);

The conversion of file “email2.msg” throws no exception, but the generated word file is blank (see attached).

The code I used was the following:

FileStream stream = File.OpenRead(@"c:\temp\email2.msg");

MapiMessage msg = MapiMessage.FromStream(stream);

MemoryStream memoryStream = new MemoryStream();

TextWriter tw = new StreamWriter(memoryStream);

tw.Write(msg.BodyRtf);

Document doc = new Document(memoryStream, null, LoadFormat.Rtf, null);

doc.Save(@"c:\temp\email2.doc", SaveFormat.Doc);

Hi,
Thanks for your detailed answer.
From your reply it seems that the issue is occurring when saving .msg file contents into word document. I believe our team of experts taking care of Aspose.Words would be in better position to answer this query. For your convenience, I am moving this thread to Aspose.Words forum.

Hi

Thanks for your request. It seems the problem occurs in your code, because you do not flush TextWriter. Please try using the following code:

MapiMessage msg = MapiMessage.FromFile(@"Test157\email2.msg");
using(MemoryStream rtfStream = new MemoryStream())
{
    // Write RTF content to the stream
    TextWriter tw = new StreamWriter(rtfStream);
    tw.Write(msg.BodyRtf);
    tw.Flush();
    // Create Docuemnt from stream
    Document doc = new Document(rtfStream);
    // Save document as DOC and As PDF
    doc.Save(@"Test157\out.doc");
    doc.SaveToPdf(@"Test157\out.pdf");
}

This code works fine with both your documents.
In addition, currently Aspose.Words allows converting to PDF without using Aspose.Pdf. Please see the following links for more information:
https://docs.aspose.com/words/net/convert-a-document-to-pdf/
So you can just try using this new method to convert your Word documents to PDF. This feature is available starting from Aspose.Words 6.0.0.
Best regards.

Hi,

Thanks for the quick response.

After your correction I can get the file “email2.msg” to work fine.

As for “email1.msg”, I no longer have an exception, but the output word document does not contain the image that was embedded in the e-mail. Is that something normal?

Best regards,
Joao

Hi

Thanks for your inquiry. There is no image in the RTF returned by Aspose.Network. you can use the following code for testing:

MapiMessage msg = MapiMessage.FromFile(@"Test157\email1.msg");
using(FileStream rtfStream = new FileStream(@"Test157\out.rtf", FileMode.Create))
{
    // Write RTF content to the stream
    TextWriter tw = new StreamWriter(rtfStream);
    tw.Write(msg.BodyRtf);
    tw.Flush();
}

So you should contact Aspose.Network team to resolve this issue.
Best regards.