HTML body is lost when MapiMEssage.SaveAsTnef is used


#1

Hi, Aspose team.

Scenario: My app processes EML messages. Some of them have TNEF format (with “application/ms-tnef” content type and winmail.dat attachment). App reads winmail.dat attachment using MapiMessage.LoadFromTnef message.

If TNEF message contains embedded message, app saves it to file (MSG format is used), processes MSG file in some way, then replaces embedded message content with the content of the processed MSG file.

Then app saves MapiMessage back to TNEF and replaces content of winmail.dat attachment.

Issue: HTML body of the embedded message is lost. Outlook show plain text body. see issue.png (59.7 KB)

Sample EML: sample.zip (876.4 KB)

using (var mailMessage = MailMessage.Load("sample for Aspose.eml",
    new EmlLoadOptions
    {
        PreserveTnefAttachments = true,
    }))
{
    using (var newTnefStream = new MemoryStream())
    {
        using (var mapiFromTnef = MapiMessage.LoadFromTnef(mailMessage.Attachments[0].ContentStream))
        {
            // for the sake of simplicity I reassign 
            // content of the embedded message
            mapiFromTnef.Attachments[0]
                .SetProperty(new MapiProperty(MapiPropertyTag.PR_ATTACH_DATA_OBJ,
                      mapiFromTnef.Attachments[0].ObjectData.Data));

            // Save MAPI message back to TNEF attachment
            mapiFromTnef.SaveAsTnef(newTnefStream);
        }

        // update content of the winmail.dat attachment
        newTnefStream.Position = 0;
        mailMessage.Attachments[0].ContentStream = newTnefStream;
        mailMessage.Save("resaved.eml", new EmlSaveOptions(MailMessageSaveType.EmlFormat));
    }
}

Thanks.


#2

@licenses,

I have worked with source file and sample code shared by you using Aspose.Email for .NET 19.3 and unable to observe HTML body issue. I have also shared my generated result with you for your kind reference. Please check attachment.resaved123.zip (875.1 KB)


#3

I opened EML file that you attached in Outlook 2016 (MS Office Professional Plus 2016) and Outlook 2013 and was able to observe the issue.

See screenshot (37.1 KB)

Thanks.


#4

@licenses,

I have worked with the issue shared by you and it seems to be an issue in API. An issue with ID EMAILNET-39440 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 fixed.


#5

Hi,

I found another issue that may be connected with the one reported above.

Scenario: User sends an email with RTF body in Exchange on-premises environment. There is custom transport agent on Exchange server. Transport agent uses Aspose.Email to do some manipulation with email. Emails have TNEF format (EML with winmail.dat).

Issue. Recipients receive corrupted email (see screenshots). It seems that RTF body is lost and plain text version is shown:

  • no test formatting in the message body;
  • attachments are listed in the attachment panel (not in the body);
  • ATTxxxxx attachment appears.

The thing is that you can replicate it only in Exchange on-premises environment when code runs in transport agent. If you simply run the code provided below in this post on the sample file (RTF sample.zip), then open resulting file in the Outlook, email will be shown OK.

It seems that changes made by Aspose.Email are critical for Exchange server.

Do you have Exchange environment? We can arrange meeting to help you install transport agent so that you can replicate the issue on your side.

Code snippet

using (var originalEmailStream = args.MailItem.GetMimeReadStream())
{
    using (var m = MailMessage.Load(originalEmailStream, new EmlLoadOptions
    {
        PreserveTnefAttachments = true,
    }))
    {
        using (var msForNewTnef = new MemoryStream())
        using (var mapi = MapiMessage.LoadFromTnef(m.Attachments[0].ContentStream))
        {

            mapi.SaveAsTnef(msForNewTnef);
            msForNewTnef.Position = 0;
            m.Attachments[0].ContentStream = msForNewTnef;

            originalEmailStream.Dispose();
            using (var ms = new MemoryStream())
            {
                m.Save(ms, new EmlSaveOptions(MailMessageSaveType.EmlFormat));
                ms.Position = 0;
                CopyStreamTo(ms, args.MailItem.GetMimeWriteStream(), true, true);
            }
        }
    }
}

Thanks.


#6

@licenses,

I have worked with the issue shared by you and it seems to be an issue in API. An issue with ID EMAILNET-39445 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 fixed.