There are a few problems with your above MIME snippet :(
Content-Transfer-Encoding: 7bit
is obviously not true, altho that's not likely to be the problem (MimeKit ignores values of 7bit
and 8bit
for this very reason).
Most importantly, however, is the fact that the charset parameter is iso-2022-jp
but the content itself is very clearly not iso-2022-jp
(it looks like utf-8
).
When you get the TextPart.Text
value, MimeKit gets that string by converting the raw stream content using the charset specified in the Content-Type
header. If that is wrong, then the Text
property will also have the wrong value.
The good news is that TextPart
has GetText methods that allow you to specify a charset override.
I would recommend trying:
var text = part.GetText (Encoding.UTF8);
See if that works.
FWIW, iso-2022-jp
is an encoding that forces Japanese characters into a 7bit ascii form that looks like complete jibberish. This is what your Japanese text would look like if it was actually in iso-2022-jp
:
BE:IU%U%!%$%kL>%F%9%H
That's how I know it's not iso-2022-jp
:)
Update:
Ultimately, the solution will probably be something like this:
var encodings = new List<Encoding> ();
string text = null;
try {
var encoding = Encoding.GetEncoding (part.ContentType.Charset,
new EncoderExceptionFallback (),
new DecoderExceptionFallback ());
encodings.Add (encoding);
} catch (ArgumentException) {
} catch (NotSupportedException) {
}
// add utf-8 as our first fallback
encodings.Add (Encoding.GetEncoding (65001,
new EncoderExceptionFallback (),
new DecoderExceptionFallback ()));
// add iso-8859-1 as our final fallback
encodings.Add (Encoding.GetEncoding (28591,
new EncoderExceptionFallback (),
new DecoderExceptionFallback ()));
for (int i = 0; i < encodings.Count; i++) {
try {
text = part.GetText (encodings[i]);
break;
} catch (DecoderFallbackException) {
// this means that the content did not convert cleanly
}
}