Skip to content

Commit

Permalink
Display non-ASCII characters correctly in emails formatted with HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
Dijji committed Nov 4, 2018
1 parent 51e3881 commit 8de40db
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
24 changes: 6 additions & 18 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,15 @@ private void ShowMessage(Message m)
// Can't bind HTML content, so push it into the control, if the message is HTML
if (m.ShowHtml)
{
string embedded = null;
string body = m.GetBodyAsHtmlString();
if (m.MayHaveInlineAttachment)
embedded = m.EmbedAttachments(xstFile); // Returns null if this is not appropriate
body = m.EmbedAttachments(body, xstFile); // Returns null if this is not appropriate

if (embedded != null)
if (body != null)
{
wbMessage.NavigateToString(embedded);
m.SortAndSaveAttachments(); // Re-sort attachments in case any new in-line rendering discovered
}
else if (m.BodyHtml != null)
{
wbMessage.NavigateToString(m.BodyHtml);
}
else if (m.Html != null)
{
var ms = new System.IO.MemoryStream(m.Html);
wbMessage.NavigateToStream(ms);
}
else if (m.Body != null)
{
wbMessage.NavigateToString(m.Body);
wbMessage.NavigateToString(body);
if (m.MayHaveInlineAttachment)
m.SortAndSaveAttachments(); // Re-sort attachments in case any new in-line rendering discovered
}
}
// Can't bind RTF content, so push it into the control, if the message is RTF
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.*")]
[assembly: AssemblyVersion("1.5.*")]
43 changes: 34 additions & 9 deletions View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,31 +183,34 @@ class Message
((BodyHtml != null && BodyHtml.Length > 0) || (Html != null && Html.Length > 0))); } }
public bool ShowRtf { get { return NativeBody == BodyType.RTF || (NativeBody == BodyType.Undefined && RtfCompressed != null && RtfCompressed.Length > 0); } }

public string EmbedAttachments(XstFile xst)
public string GetBodyAsHtmlString()
{
string raw = null;

if (BodyHtml != null)
raw = BodyHtml;
return BodyHtml; // This will be plain ASCII
else if (Html != null)
{
var e = GetEncoding();
if (e != null)
{
raw = new String(e.GetChars(Html));
return EscapeUnicodeCharacters(new String(e.GetChars(Html)));
}
}
else if (Body != null)
raw = Body;
else if (Body != null) // Not really expecting this as a source of HTML
return EscapeUnicodeCharacters(Body);

if (raw == null)
return null;
}

public string EmbedAttachments(string body, XstFile xst)
{
if (body == null)
return null;

var dict = new Dictionary<string, Attachment>();
foreach (var a in Attachments.Where(x => x.HasContentId))
dict.Add(a.ContentId, a);

return Regex.Replace(raw, @"(="")cid:(.*?)("")", match =>
return Regex.Replace(body, @"(="")cid:(.*?)("")", match =>
{
Attachment a;

Expand Down Expand Up @@ -251,6 +254,28 @@ public void SortAndSaveAttachments(List<Attachment> atts = null)
Attachments.Add(a);
}

private static string EscapeUnicodeCharacters(string source)
{
int length = source.Length;
var escaped = new StringBuilder();

for (int i = 0; i < length; i++)
{
char ch = source[i];

if (ch >= '\x00a0')
{
escaped.AppendFormat("&#x{0};", ((int)ch).ToString("X4"));
}
else
{
escaped.Append(ch);
}
}

return escaped.ToString();
}

private string EscapeString(string s)
{
var sb = new StringBuilder(s.Length);
Expand Down

0 comments on commit 8de40db

Please sign in to comment.