直到现在我都使用 OpenPop,但现在更改为 MailKit 和这两个属性:FindFirstPlainTextVersion()
并且FindFirstHtmlVersion()
MailKit 不存在。
变量 allMessages 不是 OpenPop 但现在是List<MimeKit.MimeMessage>
类型。
lvnf
是一个 ListView 控件,当我单击一个项目时,它应该显示在 RichTextBox 中,我有我单击的消息的内容。但这两个属性在MailKit(MimeKit.MimeMessage)
.
void lvnf_Click(object sender, EventArgs e)
{
OpenPop.Mime.MessagePart plainText = null;
OpenPop.Mime.MessagePart html = null;
var firstSelectedItem = ListViewCostumControl.lvnf.SelectedItems[0];
try
{
StringBuilder builder = new StringBuilder();
if (allLoadedMessages != null)
{
plainText = allLoadedMessages[firstSelectedItem.Index].FindFirstPlainTextVersion();
}
if (allMessages != null)
{
plainText = allMessages[firstSelectedItem.Index].FindFirstPlainTextVersion();
}
if (plainText != null)
{
// We found some plaintext!
builder.Append(plainText.GetBodyAsText());
}
else
{
// Might include a part holding html instead
if (allLoadedMessages != null)
{
html = allLoadedMessages[firstSelectedItem.Index].FindFirstHtmlVersion();
}
if (allMessages != null)
{
html = allMessages[firstSelectedItem.Index].FindFirstHtmlVersion();
}
if (html != null)
{
// We found some html!
builder.Append(html.GetBodyAsText());
}
}
richTextBox1.Text = builder.ToString();
}
catch(Exception err)
{
string myer = err.ToString();
}
}
到目前为止,我尝试的是创建一个新类HtmlPreviewVisitor
并从 mailkit 站点添加类示例:
然后在中使用它form1
:
void Render(MimeKit.MimeMessage message)
{
HtmlPreviewVisitor pv = new HtmlPreviewVisitor(tempDirectory);
pv.Visit(message);
var tmpDir = Path.Combine(Path.GetTempPath(), message.MessageId);
var visitor = new HtmlPreviewVisitor(tmpDir);
Directory.CreateDirectory(tmpDir);
message.Accept(visitor);
richTextBox1.Text = visitor.HtmlBody;
}
并像这样使用渲染:
void lvnf_Click(object sender, EventArgs e)
{
MimeKit.MimeMessage message = null;
var firstSelectedItem = ListViewCostumControl.lvnf.SelectedItems[0];
try
{
message = allMessages[firstSelectedItem.Index];
Render(message);
}
}
但结果richTextBox1
是我看到 html 内容而不是正文的文本。
例如:
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Magic Quadrant for aPaaS</title>
</head>
<body oncontextmenu="return false;">
<center>
<!-- Header Table For Web Version -->
<table align="center" border="0" cellpadding="10" cellspacing="0" width="660">
<tbody>
<tr>
<td align="center" style="font-family:Helvetica, Arial, sans-serif; font-size:12px; color:#333;">
To view a web version of this message, <A TARGET="_blank" HREF="http://clicks.slashdot.org/c.html?ufl=4&rtr=on&s=x8pb08,2jot2,54g,7zdr,51ie,5vra,3kxd&MLM_MID=4277846&MLM_MLID=6640&MLM_SITEID=2010001400&MLM_UNIQUEID=4300628cb1">click here</A></td>
</tr>
</tbody>
...