Wolle
Wolle

Reputation: 33

MimeKit howto base64 encode attachment filenames

MimeKit defaults to generate attachment filenames using "encoded-words"

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

I would like to get name="=?utf-8?B?VGVzdGZpbGUgd2l0aCDDpiDDuCBvZyDDpS5wZGY=?="

Here is some sample code. You can see the result in the generated eml-file

        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", "[email protected]"));
        email.Subject = "testsubject";
        email.To.Add(new MimeKit.MailboxAddress("John Doe", "[email protected]"));

        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");

Upvotes: 1

Views: 2094

Answers (1)

jstedfast
jstedfast

Reputation: 38618

You can override behavior of the parameter encoding like this:

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;

Upvotes: 1

Related Questions