我正在阅读关于如何用引用的原始消息回复HTML消息的MailKit FAQ。
它没有解释如何将消息和ReplyVisitor消息设置为正文。任何想法?
我成功地运行了示例代码,但是因为我不知道在哪里设置我的HTML消息,所以电子邮件是空白的,只有引号的消息。
public static MimeMessage Reply (MimeMessage message, MailboxAddress from, bool replyToAll)
{
var visitor = new ReplyVisitor ();
var reply = new MimeMessage ();
reply.From.Add (from);
// reply to the sender of the message
if (message.ReplyTo.Count > 0) {
reply.To.AddRange (message.ReplyTo);
} else if (message.From.Count > 0) {
reply.To.AddRange (message.From);
} else if (message.Sender != null) {
reply.To.Add (message.Sender);
}
if (replyToAll) {
// include all of the other original recipients (removing ourselves from the list)
reply.To.AddRange (message.To.Mailboxes.Where (x => x.Address != from.Address));
reply.Cc.AddRange (message.Cc.Mailboxes.Where (x => x.Address != from.Address));
}
// set the reply subject
if (!message.Subject.StartsWith ("Re:", StringComparison.OrdinalIgnoreCase))
reply.Subject = "Re: " + message.Subject;
else
reply.Subject = message.Subject;
// construct the In-Reply-To and References headers
if (!string.IsNullOrEmpty (message.MessageId)) {
reply.InReplyTo = message.MessageId;
foreach (var id in message.References)
reply.References.Add (id);
reply.References.Add (message.MessageId);
}
visitor.Visit (message);
// How do I set my own text if this is overridden by the visitor body??
// var myTextBody = new BodyBuilder();
// myTextBody.HtmlBody = "<div>HELLO WORLD</div>";
// reply.Body = myTextBody.ToMessageBody();
reply.Body = visitor.Body ?? new TextPart ("plain") { Text = ReplyVisitor.GetOnDateSenderWrote (message) + Environment.NewLine };
return reply;
}
示例回复代码仅用于生成可能填充"新消息"的文本。
编辑器窗口如果你想用编程的方式完成这个,你显然只需要把它加到ReplyVisitor.GetOnDateSenderWrote (message)
的结果后面。