Skip to content

Commit

Permalink
Make sure the correct charset is used -- #54
Browse files Browse the repository at this point in the history
  • Loading branch information
andyedinborough committed Apr 8, 2012
1 parent e186bd8 commit ab4975f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions HeaderObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ public string Charset {
public string Body { get; set; }

internal void SetBody(string value) {
var encoding = Utilities.ParseCharsetToEncoding(Charset);
if (ContentTransferEncoding.Is("quoted-printable")) {
value = Utilities.DecodeQuotedPrintable(value, Utilities.ParseCharsetToEncoding(Charset));
value = Utilities.DecodeQuotedPrintable(value, encoding);

} else if (ContentTransferEncoding.Is("base64")
//only decode the content if it is a text document
&& ContentType.StartsWith("text/", StringComparison.OrdinalIgnoreCase)
&& Utilities.IsValidBase64String(value)) {
var data = Convert.FromBase64String(value);
using (var mem = new System.IO.MemoryStream(data))
using (var str = new System.IO.StreamReader(mem, true))
using (var str = new System.IO.StreamReader(mem, encoding))
value = str.ReadToEnd();

ContentTransferEncoding = string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion TextClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class TextClient : IDisposable {
public System.Text.Encoding Encoding { get; set; }

public TextClient() {
Encoding = Utilities.ParseCharsetToEncoding("ISO-8859-1");
Encoding = Utilities.ParseCharsetToEncoding(Utilities.DEFAULT_CHARSET);
}

internal abstract void OnLogin(string username, string password);
Expand Down
4 changes: 3 additions & 1 deletion Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace AE.Net.Mail {
internal static class Utilities {
public const string DEFAULT_CHARSET = "ISO-8859-1";

internal static void TryDispose<T>(ref T obj) where T : class, IDisposable {
try {
if (obj != null)
Expand Down Expand Up @@ -207,7 +209,7 @@ internal static string DecodeWords(string encodedWords) {
/// <exception cref="ArgumentNullException">If <paramref name="characterSet"/> is <see langword="null"/></exception>
public static Encoding ParseCharsetToEncoding(string characterSet) {
if (string.IsNullOrEmpty(characterSet))
return null;
characterSet = DEFAULT_CHARSET;

string charSetUpper = characterSet.ToUpperInvariant();
if (charSetUpper.Contains("WINDOWS") || charSetUpper.Contains("CP")) {
Expand Down

0 comments on commit ab4975f

Please sign in to comment.