MimeKit howto base64编码附件文件名

时间:2019-09-10 10:19:27

标签: c# mimekit

MimeKit默认使用“编码字”生成附件文件名

“带有æøogå.pdf的测试文件”变为 名称* = 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");

1 个答案:

答案 0 :(得分:0)

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

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;