[英]MimeKit subject encoding and Cyrillic alphabet
I'm using MimeKit to build email body string then pass it to pmta along with list of receivers + placeholders for every receiver, then pmta takes care of merging them.我正在使用 MimeKit 构建电子邮件正文字符串,然后将其与每个接收者的接收者列表 + 占位符一起传递给 pmta,然后 pmta 负责合并它们。 We're missing placeholders in subject when the subject also contains cyrillic characters.
当主题还包含西里尔字符时,我们缺少主题中的占位符。
Placeholders are expressed as strings in square brackets, ie [firstname]
占位符表示为方括号中的字符串,即
[firstname]
Sample subject with two tokens and few characters in between:具有两个标记和中间几个字符的示例主题:
subject = "Message for [firstname] Агф [firstname]";
generates the following string (note the second placeholder, missing because of encoding):生成以下字符串(注意第二个占位符,由于编码而丢失):
From: ...
Date: ...
Subject: Message for [firstname]
=?utf-8?b?0JDQs9Cw0YTQvtC90L7QsiDQhtC70LvRjyBbZmlyc3RuYW1lXSDQhtC70LvRjw==?=
This is how I'm building the message:这就是我构建消息的方式:
Encoding useEncoding = Encoding.UTF8;
subject = subject.Replace("\u00A0", " "); // replace nbsp with normal space in case one copied by mistake
var message = new MimeMessage() { Subject = subject };
var bodyText = StripHtmlBody(body);
var textPart = new TextPart(MimeKit.Text.TextFormat.Plain)
{
ContentTransferEncoding = ContentEncoding.QuotedPrintable
};
textPart.SetText(useEncoding, bodyText);
var htmlPart = new TextPart(MimeKit.Text.TextFormat.Html)
{
ContentTransferEncoding = ContentEncoding.QuotedPrintable
};
htmlPart.SetText(useEncoding, body);
var multipartAlternative = new MultipartAlternative()
{
textPart,
htmlPart
};
message.Body = multipartAlternative;
foreach (var item in headers)
message.Headers.Add(item.HeaderId, item.HeaderValue);
var tokenMailbox = MailboxAddress.Parse("non-existing-email@domain.com");
message.To.Add(tokenMailbox);
message.Prepare(EncodingConstraint.EightBit, 998);
var messageString = "";
using (var ms = new MemoryStream())
{
message.WriteTo(ms);
messageString = useEncoding.GetString(ms.ToArray());
}
messageString = messageString.Replace(tokenMailbox.ToString(), "[ReceiverMailbox]");
// inject in pmta and return...
Message body (text & html) is persisted okay, tokens are there after encryption and everything works as expected, my issue with this is at the subject part.消息正文(文本和 html)可以保留,加密后令牌在那里,一切都按预期工作,我的问题在于主题部分。
Is there any advice on how can I overcome this situation where certain tokens/placeholders would be spared by encoding?关于如何克服通过编码避免某些令牌/占位符的这种情况,有什么建议吗?
Any help is appreciated.任何帮助表示赞赏。
在执行任何字符串替换操作之前,您需要对主题进行解码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.