Hi team,
We use 19.11, 20.1 and 20.2 AsposeEmail.dll.
Step to reproduce:
Try to receive message (e.g Subj é.zip (12.9 KB)
) with French character é in the message body…
We use this code to include embedded resource to HTML:
using (MemoryStream ms = new MemoryStream())
{
HtmlSaveOptions htmlSaveOption = new HtmlSaveOptions();
Hi @mudassir.fayyaz,
I requested that you try receiving this message… not load from file…
You set TextEncoding manually before loading email from file…
Try to forward and receive email with another character set it and the behavior will be different …
So how could I get the right character set from message?
As I understand I can use message.BodyEncoding.WindowsCodePage to get the right result… is that what it is?:
I have observed your comments and like to share that even if I comment out the following line in my provided sample code the rendering is done right. There is no issue with French character.
Try to load this email Sushma_test.zip (2.8 KB)
from file. When you use MapiMessage class you get wrong html presentation like image.png (40.5 KB)
When you use MailMessage class and load the same email you get wrong html presentation in case you commented encoding line: image.png (40.3 KB)
but with hard coded text encoding load option your rendering looks right: image.png (40.0 KB)
So try to remove these lines and receive email from network you get wrong view:
Could you answer my previous questions:
Does message.BodyEncoding.WindowsCodePage contain a message character set?
Could you clarify if there’s any other way to include embedded resources to HTML?
and
Why MapiMessage and MailMessage have not similar behavior while loading email to stream?
Where and how can I get right character set to set it as content encoding?
Yes, if you don’t set explicit encoding setting emlLoadOptions.PrefferedTextEncoding = Encoding.GetEncoding(“iso-8859-1”);
If you set explicit encoding then this setting contains value which you set.
Exsists 2 ways to embed resources
htmlSaveOption.EmbedResources = true; - then attachments and resources will be embed as base64 string in html
Instead you can correctly convert from bytes to string:
Encoding enc = message.BodyEncoding ?? message.PreferredTextEncoding;
string html = enc.GetString(ms.ToArray());
We have investigated the requirements further on our end.
Aspose.Email API allows you to embed resources when saving messages to a HTML file or stream. If you do not want to save the message to a file or stream, then where do you want to embed these resources? Can you please elaborate.
In the following code in comments we he shared explanations concerning different behavior of MailMessage and MapiMessage , and correct getting string from bytes.
HtmlSaveOptions htmlSaveOption = new HtmlSaveOptions();
htmlSaveOption.HtmlFormatOptions |= HtmlFormatOptions.WriteHeader |
HtmlFormatOptions.WriteCompleteEmailAddress |
HtmlFormatOptions.WriteCompleteCcEmailAddress;
htmlSaveOption.CheckBodyContentEncoding = true;
htmlSaveOption.EmbedResources = true;
MsgLoadOptions emlLoadOptions = new MsgLoadOptions();
MailMessage eml = MailMessage.Load(fileName, emlLoadOptions);
MemoryStream msEml = new MemoryStream();
eml.Save(msEml, htmlSaveOption);
//incorrect, saved as UTF8, gets by ASCII
string html1 = Encoding.ASCII.GetString(msEml.ToArray());
//incorrect saved by UTF8, got by UTF16
string html2 = Encoding.GetEncoding(eml.BodyEncoding.WindowsCodePage).GetString(msEml.ToArray());
//correct because encoding choosed correctly
string html3 = eml.BodyEncoding.GetString(msEml.ToArray());
emlLoadOptions.PrefferedTextEncoding = Encoding.GetEncoding("iso-8859-1");
MailMessage eml2 = MailMessage.Load(fileName, emlLoadOptions);
MemoryStream msEml2 = new MemoryStream();
eml2.Save(msEml2, htmlSaveOption);
//all strings are correct because encoding set explicit and character is suitable
string html11 = Encoding.ASCII.GetString(msEml2.ToArray());
string html22 = Encoding.GetEncoding(eml2.BodyEncoding.WindowsCodePage).GetString(msEml2.ToArray());
string html33 = eml2.BodyEncoding.GetString(msEml2.ToArray());
MapiMessage msg = MapiMessage.FromFile(fileName);
MemoryStream msMsg = new MemoryStream();
//encoding "iso-8859-1" setting not applied because MapiMessage.FromFile method does not contain emlLoadOptions as parameter
msg.Save(msMsg, htmlSaveOption);
///incorrect, the similar behaviour as eml without explicit encoding setting
string html111 = Encoding.ASCII.GetString(msMsg.ToArray());
string html222 = Encoding.GetEncoding(eml.BodyEncoding.WindowsCodePage).GetString(msEml.ToArray());
//correct because encoding choosed correctly
string html333 = Encoding.GetEncoding(msg.CodePage).GetString(msMsg.ToArray());