我正在使用 MimeKit 构建电子邮件正文字符串,然后将其与接收者列表和每个接收者的占位符一起传递给 pmta,然后 pmta 负责合并它们。当主题还包含西里尔字符时,我们在主题中缺少占位符。
占位符用方括号中的字符串表示,即[firstname]
带有两个标记和中间几个字符的示例主题:
subject = "Message for [firstname] Агф [firstname]";
生成以下字符串(注意第二个占位符,由于编码而丢失):
From: ...
Date: ...
Subject: Message for [firstname]
=?utf-8?b?0JDQs9Cw0YTQvtC90L7QsiDQhtC70LvRjyBbZmlyc3RuYW1lXSDQhtC70LvRjw==?=
这就是我构建消息的方式:
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...
消息正文(文本和 html)保持不变,加密后令牌在那里,一切都按预期工作,我的问题在于主题部分。
关于如何克服这种情况,如果某些令牌/占位符可以通过编码来避免,有什么建议吗?
任何帮助表示赞赏。