简体   繁体   English

如何使用 MimeKit/MailKit 设置自定义内容传输编码?

[英]How to set custom content transfer encoding using MimeKit/MailKit?

This question is about what I think is "breaking the standard" and I understand that it's possible MimeKit is setup to specifically not allow me to do the things I'm asking.这个问题是关于我认为什么是“打破标准”,我知道 MimeKit 可能被设置为特别不允许我做我要求的事情。 These custom messages will be only used internally and not for regular email sending.这些自定义消息将仅在内部使用,而不用于常规电子邮件发送。

The below is the attachment I'm able to create using the basic functions:以下是我可以使用基本功能创建的附件:

Content-Type: application/octet-stream; name=example.txt
Content-Disposition: attachment; filename=example.txt
Content-Transfer-Encoding: base64

**BASE64 ENCODED ATTACHMENT**

What I would like to know is if it's possible to create the following:我想知道的是是否可以创建以下内容:

Content-Type: application/octet-stream; name=example.txt; type=****
Content-Disposition: attachment; filename=example.txt
Content-Transfer-Encoding: *****

**CUSTOM ENCODED ATTACHMENT**

Where I have a custom string to set the "Content-Transfer-Encoding", possibly a custom "type" under "Content-Type", and also use my own custom code to encode the message.我有一个自定义字符串来设置“内容传输编码”,可能是“内容类型”下的自定义“类型”,并且还使用我自己的自定义代码对消息进行编码。

I'm assuming that the easiest way to custom encode my message is to do this outside of MimeKit, and then set MimeKit to not encode.我假设对我的消息进行自定义编码的最简单方法是在 MimeKit 之外执行此操作,然后将 MimeKit 设置为不编码。 Is there anyway that I can create custom headers that just contain strings I want?无论如何,我可以创建只包含我想要的字符串的自定义标头吗?

Extra Question:额外问题:

How would I go about changing:我将如何改变:

Content-Type: application/octet-stream; name=example.txt
Content-Disposition: attachment; filename=example.txt

to:到:

Content-Type: application/octet-stream; name="example.txt"
Content-Disposition: attachment; filename="example.txt"

As you've discovered in your own answer, you can override the Content-Transfer-Encoding header by using Headers.Replace() or even Headers.Add() if you don't set the ContentTransferEncoding property.正如您在自己的答案中发现的那样,如果您未设置ContentTransferEncoding属性,则可以使用 Headers.Replace() 甚至 Headers.Add() 覆盖 Content-Transfer-Encoding 标头。

I guess your other main question is how do you get the custom encoded content?我想你的另一个主要问题是你如何获得自定义编码的内容?

Instead of doing:而不是做:

Content = new MimeContent(File.OpenRead(file), ContentEncoding.Default),

All you need to do is pass in a pre-encoded stream into the MimeContent .ctor (and continue using ContentEncoding.Default as the second parameter).您需要做的就是将预先编码的流传入MimeContent .ctor(并继续使用ContentEncoding.Default作为第二个参数)。

If you need to set a type parameter in the Content-Type header, you can do this:如果您需要在 Content-Type 标头中设置type参数,您可以这样做:

attachment.ContentType.Parameters.Add("type", "value");

or或者

var parameter = new Parameter ("name", "value");
attachment.ContentType.Parameters.Add (parameter);

or或者

attachment.ContentType.Parameters["type"] = "value";

The only question remaining is how to enforce parameter values being quoted.剩下的唯一问题是如何强制引用参数值。 For that, you are out of luck.为此,你运气不好。 MimeKit will only quote the value if it needs to be quoted due to the characters in the value.如果由于值中的字符需要引用,MimeKit 只会引用该值。

I found my one of my answers is replacing headers:我发现我的答案之一是替换标题:

var attachment = new MimePart("application", "octet-stream")
{
    Content = new MimeContent(File.OpenRead(file), ContentEncoding.Default),
    ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
    ContentTransferEncoding = ContentEncoding.Base64,
    FileName = Path.GetFileName(file),
};

attachment.Headers.Replace("Content-Transfer-Encoding", "******");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM