MimeKit howto base64 编码附件文件名

MimeKit howto base64 encode attachment filenames

MimeKit 默认使用 "encoded-words"

生成附件文件名

"Testfile with æ ø og å.pdf" 变成 name*=iso-8859-1''Testfile%20with%20%E6%20%F8%20og%20%E5.pdf

我想得到 name="=?utf-8?B?VGVzdGZpbGUgd2l0aCDDpiDDuCBvZyDDpS5wZGY=?="

这是一些示例代码。 在生成的eml文件中可以看到结果

        MimeKit.AttachmentCollection vedhaeftedefiler = new MimeKit.AttachmentCollection();

        vedhaeftedefiler.Add(@"d:\temp\Testfile with æ ø og å.pdf");

        var email = new MimeKit.MimeMessage();
        email.From.Add(new MimeKit.MailboxAddress("John Doe", "john@example.com"));
        email.Subject = "testsubject";
        email.To.Add(new MimeKit.MailboxAddress("John Doe", "john@example.com"));

        var bodyBuilder = new MimeKit.BodyBuilder();
        bodyBuilder.TextBody = "Hello John";

        foreach (var a in vedhaeftedefiler)
        {
            bodyBuilder.Attachments.Add(a);
        }
        email.Body = bodyBuilder.ToMessageBody();
        email.Body.Prepare(MimeKit.EncodingConstraint.EightBit);
        email.WriteTo(@"d:\temp\MimeKitTest_ " + DateTime.Now.ToString("yyMMdd hhmmss") + ".eml");

您可以像这样覆盖参数编码的行为:

var attachments = new AttachmentCollection ();
var attachment = attachments.Add(@"d:\temp\Testfile with æ ø og å.pdf");

foreach (var parameter in attachment.ContentType.Parameters)
    parameter.EncodingMethod = ParameterEncodingMethod.Rfc2047;

foreach (var parameter in attachment.ContentDisposition.Parameters)
    parameter.EncodingMethod = ParameterEncodingMethod.Rfc2047;