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.84k stars 373 forks source link

Base64 encoded file name not parsed #1095

Closed martinknafvework closed 2 weeks ago

martinknafvework commented 2 weeks ago

Describe the bug

When loading an email using MimeMessage.Load, attachments containing base64-encoded utf8 file names are not parsed properly.

Platform (please complete the following information):

To Reproduce

  1. Download the attached email.txt.
  2. Parse the file using the code below.

Now you will see the attachment filename =?UTF-8?B?SGVsbG9Xb3JsZC50eHQ=?= printed to console.

Expected behavior I expect to see the attachment file name HelloWorld.txt printed to the console. This is what is shown if I open the same file in Outlook or Mozilla Thunderbird.

Code Snippets If applicable, add code snippets to help explain your problem.

 MimeMessage message;
 using (var stream = File.OpenRead(filePath))
 {
    message = MimeMessage.Load(stream);
 }

 foreach (var attachment in message.Attachments)
 {
    if (attachment is MimePart mimePart && mimePart.FileName != null)
    {
       Console.WriteLine("Attachment filename: " + mimePart.FileName);
    }
 }

Additional context

jstedfast commented 2 weeks ago

The issue is the mix-and-match encoding rules that your sample email uses. It uses both rfc2231 and rfc2047 encoding rules when it should only use rfc2231 -or- rfc2047 (technically, it should use rfc2231 since that is the standard, but rfc2047 encodings were used in the 90's because rfc2231 didn't exist yet).

Instead, the client that sent this message used rfc2047 encoding and then passed that through the rfc2231 encoding rules.

I don't think this is a case worth working around because I guarantee whatever client sent this is the only client in existence that does this.