有没有办法将MimeKit.MimeMessage转换为可以在Web浏览器中呈现的HTML?我不关心邮件附件,但希望能够在浏览器中显示邮件正文,包括嵌入的图像.我是MimeKit的新手,无法在API文档中找到任何内容.任何信息表示赞赏.
编辑:我没有找到与MimeKit本地执行此操作的方法,但我将其与HtmlAgilityPack结合以解析MimeMessage.HtmBody并修复内嵌图像.这似乎有效,除非有人有更好的主意,否则我会继续使用它.供参考,这是代码:
//////////////////////////////////////////////////////////////////////////////////////////
// use MimeKit to parse the message
//////////////////////////////////////////////////////////////////////////////////////////
MimeKit.MimeMessage msg = MimeKit.MimeMessage.Load(stream);
//////////////////////////////////////////////////////////////////////////////////////////
// use HtmlAgilityPack to parse the resulting html in order to fix inline images
//////////////////////////////////////////////////////////////////////////////////////////
HtmlAgilityPack.HtmlDocument hdoc = new HtmlAgilityPack.HtmlDocument();
hdoc.LoadHtml(msg.HtmlBody);
// find all image nodes
var images = hdoc.DocumentNode.Descendants("img");
foreach (var img in images)
{
// check that this is an inline image
string cid = img.Attributes["src"].Value;
if (cid.StartsWith("cid:"))
{
// remove the cid part of the attribute
cid = cid.Remove(0, 4);
// …Run Code Online (Sandbox Code Playgroud)