简体   繁体   中英

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. We're missing placeholders in subject when the subject also contains cyrillic characters.

Placeholders are expressed as strings in square brackets, ie [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.

Is there any advice on how can I overcome this situation where certain tokens/placeholders would be spared by encoding?

Any help is appreciated.

在执行任何字符串替换操作之前,您需要对主题进行解码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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