Hello,
Here i attach my findings with an issue trying to export to HTML a MapiMessage.
The message contains cyrillic characters in the header (“Cc”, “From”, “To”), mapiMessage loads them correctly (attached debugging that proves) but when i try to export the object to HTML (with the options attached as well in document), those cyrillic characters are replaced by ‘?’. I tried with different setting combinations of htmlSaveOptions class but none of them with success.
Using com.aspose:aspose-email:jdk16:22.2
Please can you give me any hints regards this issue?
Thanks in advance for support!
The issue with Cyrillic characters being replaced by ‘?’ during the export of a MapiMessage to HTML is likely due to encoding problems. This can occur if the HTML export process does not correctly handle the character encoding for Cyrillic characters.
Solution
To resolve this issue, you should ensure that the HtmlSaveOptions class is configured to use the correct encoding. Specifically, you can set the encoding to UTF-8, which supports Cyrillic characters.
Code
Here’s a code snippet that demonstrates how to set the encoding in HtmlSaveOptions:
import com.aspose.email.MapiMessage;
import com.aspose.email.HtmlSaveOptions;
public class ExportToHtml {
public static void main(String[] args) {
MapiMessage mapiMessage = MapiMessage.load("path/to/your/message.msg");
HtmlSaveOptions options = new HtmlSaveOptions();
options.setEncoding("UTF-8"); // Set encoding to UTF-8
mapiMessage.save("output.html", options);
}
}
Next Steps
Implement the above code and ensure that the HtmlSaveOptions is set to UTF-8.
Test the export again to see if the Cyrillic characters are preserved correctly.
If the issue persists, check the source of the MapiMessage to ensure it is correctly encoded before export.
Please proceed with these steps, and let me know if you encounter any further issues!
Sorry but the message is confidential so I cannot share it, but I guess you can replicate the issue with a regular message that contains cyrillic characters in the contact names inside Header fields (To, Cc, From, …).
Hi again, any hints how to check/debug the header rendering part? how do I check enconding for header, as body part it generated cyrillic characters without issues, even in the same MSG File
Our devs is currently analyzing the reported behavior and attempting to reproduce the issue. We’ll get back to you as soon as we have information from them.
We have tested this case with Cyrillic characters in contact names inside header fields and exported to HTML, but we could not reproduce the issue on our side. We verified this with version 22.2 as well as with the latest version.
If possible, could you please save a .msg file containing data that does not include confidential information but still reproduces the issue on your side? This will help us investigate and assist you more efficiently.
As part of our investigation, we used the code you initially shared and also extended the test with an additional export scenario using MapiMessage.toMailMessage() and MailConversionOptions:
String msgFile = "test.msg";
MapiMessage msg = MapiMessage.load(msgFile);
HtmlSaveOptions htmlSaveOptions = SaveOptions.getDefaultHtml();
htmlSaveOptions.setHtmlFormatOptions(HtmlFormatOptions.WriteHeader | HtmlFormatOptions.DisplayAsOutlook);
htmlSaveOptions.setCheckBodyContentEncoding(true);
htmlSaveOptions.setEmbedResources(false);
htmlSaveOptions.setMailMessageSaveType(MailMessageSaveType.getHtmlFormat());
htmlSaveOptions.setResourceRenderingMode(ResourceRenderingMode.EmbedIntoHtml);
htmlSaveOptions.setRenderedContactFields(ContactFieldsSet.AllExisting);
// Export directly from MapiMessage
msg.save(msgFile + "_1.html", htmlSaveOptions);
// Convert to MailMessage and export again
MailConversionOptions conversionOptions = new MailConversionOptions();
conversionOptions.setKeepOriginalEmailAddresses(true);
MailMessage mailMessage = msg.toMailMessage(conversionOptions);
mailMessage.save(msgFile + "_2.html", htmlSaveOptions);
Could you please try this code on your side and check whether either of the two resulting HTML files (_1.html or _2.html) displays Cyrillic characters correctly?
This will help us narrow down the root cause of the issue.
Thanks! Second one works! It does display Cyrillic chars correctly!
What is the difference? I need to know as we have post mail cleanup processing logic so I need to adapt our code accordingly.
When a message is saved from Outlook in ASCII format, certain header fields (like To, Cc, From) may lose non-ASCII characters (such as Cyrillic), as Outlook does not always preserve full Unicode information in all MAPI properties.
However, Outlook also stores a raw MIME representation of the headers in the PR_TRANSPORT_MESSAGE_HEADERS MAPI property. This field is typically generated during message transport (e.g., when the message is sent), and it contains a complete copy of the original headers in MIME format, including correct encodings.
When you convert the MapiMessage to a MailMessage using toMailMessage(), Aspose.Email checks for the presence of PR_TRANSPORT_MESSAGE_HEADERS and reconstructs the header fields (such as To, Cc, From) using the MIME-compliant data stored there. This is why Cyrillic characters are correctly restored in your second export scenario.