Search code examples
mimemailkit

Content encoding using MimeKit/MailKit


We are encountering some encoding issues, specially when using 8bit as content transfer encoding. First of all, can anyone please tell me how 8bit encoded value of a-umlaut looks like?

What is best in practice to handle encoding?

I tried to use the WriteTo() method of a MIME entity, to write the content into a stream, which works in any cases other than with 8bit encoding.

UPDATE: Currently using the code as posted in one of the examples of MimeKit:

using (MemoryStream memStm = new MemoryStream())
{
    mime.WriteTo(memStm);
    message.MimeMessage = Encoding.UTF8.GetString(memStm.ToArray());
}

But it seems like some kind of double encoding when my MIME contains special characters like: äÄ will result in something like: ¿½

How can I escape those double encoding situations?


Solution

  • The 8-bit MIME transfer encoding is basically "no encoding", so any MIME data encoded with 8-bit encoding is the same as the binary representation of the data in the given charset. For instance, 'ä' represented in UTF-8 as the following sequence of bytes: 0xC3, 0xA4. When using 8-bit, your MIME data will be the very same sequence of bytes. Other transfer encodings like quoted-printable or base64 will encode those bytes differently, e.g. as w6Q= or =C3=A4.

    The takeaway is that the MIME character set specifies how characters are represented in binary form and the MIME content transfer encoding specifies how those bytes get encoded in the MIME document itself.

    As for best practices, modern email servers and clients will happily deal with 8-bit encoded emails. Still, the custom is to use either quoted-printable or base64.

    As for the double-encoding issue, the sequence äÄ double UTF-8 encoded looks different from ¿½, so I thinks something else is going wrong there. I am not familiar with MimeKit and your code sample does not contain enough information, but if you update your question with more complete repro code, I will be happy to update my answer.