jstedfast / MimeKit

A .NET MIME creation and parser library with support for S/MIME, PGP, DKIM, TNEF and Unix mbox spools.
http://www.mimekit.net
MIT License
1.82k stars 369 forks source link

RFC2047 DecodeText issue #1009

Closed MacDisein closed 7 months ago

MacDisein commented 7 months ago

I have a problem with the subject of a MIME message. The header field is not decoded correctly.

The RawValue of the field is:

=?iso-8859-2?q?AW=3A_Dostawa_=BFelatyny_=282361=29_PO?=
 =?iso-8859-2?q?_4500367149?=

The decoded result from the MimeKit is: AW: Dostawa ¿elatyny (2361) PO 4500367149

The correct result should look like this: AW: Dostawa żelatyny (2361) PO 4500367149

Am I doing something wrong or is this actually a bug?

I am using MimeKit 4.3.0 on Windows with .NET 8

jstedfast commented 7 months ago

How are you decoding it? I added it as a unit test and it works fine:

[Test]
public void TestDecodeIso88592 ()
{
    const string text = "=?iso-8859-2?q?AW=3A_Dostawa_=BFelatyny_=282361=29_PO?=\r\n =?iso-8859-2?q?_4500367149?=";
    const string expected = "AW: Dostawa żelatyny (2361) PO 4500367149";
    var buffer = Encoding.UTF8.GetBytes (text);
    string result;
    int codepage;

    result = Rfc2047.DecodeText (ParserOptions.Default, buffer, 0, buffer.Length, out codepage);
    Assert.That (result, Is.EqualTo (expected), "DecodeText");
    Assert.That (codepage, Is.EqualTo (28592), "DecodeText");

    result = Rfc2047.DecodeText (ParserOptions.Default, buffer, 0, buffer.Length);
    Assert.That (result, Is.EqualTo (expected), "DecodeText");

    result = Rfc2047.DecodeText (ParserOptions.Default, buffer);
    Assert.That (result, Is.EqualTo (expected), "DecodeText");

    result = Rfc2047.DecodeText (buffer);
    Assert.That (result, Is.EqualTo (expected), "DecodeText");
}
jstedfast commented 7 months ago

Hmmm, just remembered something.

Did you remember to register non-Unicode text encodings?

System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);
MacDisein commented 7 months ago

That's it!!

Great - thank you!