How to decode a quoted printable e-mail header (with MimeKit)

1.4k views Asked by At

I have this header from an e-mail message: (some headers only, from a log file, not the entire message)

From: =?UTF-8?B?TmFtZSDDpMO2w7w=?= <[email protected]>

How can I decode the part after the "From:" into the Unicode string that it was when I entered it into Thunderbird to send the message?

Other relevant occurrences are the Subject header where I think I've already seen something like this:

Subject: Hello =?UTF-8?B?TmFtZSDDpMO2w7w=?= more words

I already found the QuotedPrintableDecoder class in MimeKit but can't find out how to use that thing. It seems to want me to let it guess the decoded byte count and then convert the bytes from the hopefully large enough buffer into a string, but doesn't tell me what Encoding to use.

I need a short example of how to convert such header strings that contain partial quoted-printable words including a specified encoding. An example of the kind that one would expect to find in the documentation. Actually all data is right here but needs to be converted from one string to another.

Another hack using Attachment.CreateAttachmentFromString fails here. It can decode entire QP strings but not mixed formats like this one.

PS: If there are solutions in .NET Core 3.1 or 5.0 without the need for MimeKit, I'll happily accept those as well!

1

There are 1 answers

3
jstedfast On BEST ANSWER

When you parse a message with MimeMessage.Load(), you don't need to decode headers because MimeKit will do it for you.

Secondly, your example header is not encoded using quoted-printable, it's encoded using rfc2047 tokens which would need to be decoded using Rfc2047.DecodeText():

var decoded = Rfc2047.DecodeText (Encoding.ASCII.GetBytes ("Hello =?UTF-8?B?TmFtZSDDpMO2w7w=?= more words"));