The images are lost when converting the message to MIME, when converting an RTF that contains inline Images from a PST file


#1

class Program
{
static void Main(string[] args)
{
License license = new License();

        license.SetLicense(@"c:\Aspose.Email.lic");

        var pstFilename = "c:\\pstFile.pst";


        using (var pst = PersonalStorage.FromFile(pstFilename))
        {
            var inbox = pst.RootFolder.EnumerateFolders().Single(x => x.DisplayName == "Inbox");

            var msg = inbox.EnumerateMapiMessages().Single();

            File.WriteAllBytes("c:\\resultMime.eml", ConvertAsposeMapiMessageToRawMime(msg));

            var reloaded = SaveToMSGAndReload(msg);

            File.WriteAllBytes("c:\\resultMimeAfterReloading.eml", ConvertAsposeMapiMessageToRawMime(reloaded));
        }
    }

    public static MapiMessage SaveToMSGAndReload(MapiMessage msg)
    {
        var stream = new MemoryStream();

        msg.Save(stream, SaveOptions.DefaultMsgUnicode);

        stream.Position = 0;

        return MapiMessage.FromStream(stream);
    }

    public static byte[] ConvertAsposeMapiMessageToRawMime(MapiMessage msg)
    {
        var mailMessage = msg.ToMailMessage(
            new MailConversionOptions
            {
                ConvertAsTnef = false
            });

        var tempMemoryStream = new MemoryStream();

        mailMessage.Save(
            tempMemoryStream,
            new EmlSaveOptions(MailMessageSaveType.EmlFormat)
            {
                MailMessageSaveType = MailMessageSaveType.EmlFormat,
                PreserveSignedContent = true
            });

        return tempMemoryStream.ToArray();
    }
}

I also attached a sample PST file.

In the example above, c:\resultMime.eml will not contain the images, while c:\resultMimeAfterReloading.eml contains the images.

PstFile.zip (4.5 MB)


#2

@bahakh,

I have worked with your sample code and suggest you to please add following modification in your sample code.

        public static byte[] ConvertAsposeMapiMessageToRawMime(MapiMessage msg)
        {
            var mailMessage = msg.ToMailMessage(
                new MailConversionOptions
                {
//                    ConvertAsTnef = false
                    ConvertAsTnef = true
                });

            var tempMemoryStream = new MemoryStream();

            mailMessage.Save(
                tempMemoryStream,
                new EmlSaveOptions(MailMessageSaveType.EmlFormat)
                {
                    MailMessageSaveType = MailMessageSaveType.EmlFormat,
                    PreserveSignedContent = true,
                
                });

            return tempMemoryStream.ToArray();
        }

#3

We know about this flag. We do not wish to convert as TNEF.


#4

@bahakh,

The original message uses RTF format of body. When message converting to eml, the body is converted from RTF to HTML format. Sometimes complicated formatting is lost during this conversion. When converting to TNEF EML (PreserveTnefAttachments = true), the original message is wrapped into TNEF without conversion. This allows you to keep the original properties, so use of the PreserveTnefAttachments flag is preferable at msg to eml conversion. Otherwise, it may take a long time to fix the conversion of RTF-> HTML, and there are no guarantees that similar problems will not arise in the future.
Please share your feedback whether you are satisfied with the use of the PreserveTnefAttachments flag or exists some reasons (specify please) that prevent you from using this flag.

I hope the shared elaboration will be helpful.


#5

Thank you for the quick reply… But Why does the issue not occur if we first save to MSG?


#6

@bahakh,

I have observed your comments and like to share that the issue does not occur when you save first as MSG because in MSG format Outlook uses RTF body without converting to HTML. I hope the shared information will be helpful.


#7

So a MapiMessage inside a PST converted to EML -> does not work correctly

But a MapiMessage Inside PST saved to MSG file and then loaded into a MapiMessage and then converted to EML -> works correctly.

Both PST and MSG support having an RTF body.

And in both cases, the conversion happens from something that supports a RTF body to EML. I don’t understand why there is a difference in behavior.

I think this is a bug.


#8

@bahakh,

I have observed your comments and an issue with ID EMAILNET-39574 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be addressed.