From e186bd8df3822d8e65a8b0f4d745576f26cd015d Mon Sep 17 00:00:00 2001 From: Andy Edinborough Date: Sun, 8 Apr 2012 11:17:58 -0500 Subject: [PATCH] Potential fix for #54; ensures the `Encoding` property of the ImapClient is used, and default the charset to `ISO-8859-1`. --- ImapClient.cs | 28 ++++++++++++++++------------ TextClient.cs | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/ImapClient.cs b/ImapClient.cs index d16bb7a..e3e3024 100644 --- a/ImapClient.cs +++ b/ImapClient.cs @@ -424,20 +424,24 @@ public MailMessage[] GetMessages(string start, string end, bool uid, bool header foreach (var key in imapHeaders.AllKeys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase)) mail.Headers.Add(key, new HeaderValue(imapHeaders[key])); - var body = new StringBuilder(); - int remaining = mail.Size; - var buffer = new byte[8192]; - int read; - while (remaining > 0) { - read = _Stream.Read(buffer, 0, Math.Min(remaining, buffer.Length)); - body.Append(System.Text.Encoding.UTF8.GetString(buffer, 0, read)); - remaining -= read; - } + using (var body = new System.IO.MemoryStream()) { + int remaining = mail.Size; + var buffer = new byte[8192]; + int read; + while (remaining > 0) { + read = _Stream.Read(buffer, 0, Math.Min(remaining, buffer.Length)); + body.Write(buffer, 0, read); + remaining -= read; + } - var next = Convert.ToChar(_Stream.ReadByte()); - System.Diagnostics.Debug.Assert(next == ')'); + var next = Convert.ToChar(_Stream.ReadByte()); + System.Diagnostics.Debug.Assert(next == ')'); - mail.Load(body.ToString(), headersonly); + body.Position = 0; + using (var rdr = new System.IO.StreamReader(body, Encoding)) { + mail.Load(rdr, headersonly); + } + } x.Add(mail); } diff --git a/TextClient.cs b/TextClient.cs index a04e130..e66ba9f 100644 --- a/TextClient.cs +++ b/TextClient.cs @@ -16,7 +16,7 @@ public abstract class TextClient : IDisposable { public System.Text.Encoding Encoding { get; set; } public TextClient() { - Encoding = System.Text.Encoding.UTF8; + Encoding = Utilities.ParseCharsetToEncoding("ISO-8859-1"); } internal abstract void OnLogin(string username, string password);