[英]How can i parse html message body using MailKit?
I used OpenPop until now but now change to MailKit and this two properties: FindFirstPlainTextVersion()
and FindFirstHtmlVersion()
not exist with MailKit. 到目前为止,我一直使用OpenPop,但现在更改为MailKit,并且这两个属性:MailKit不存在
FindFirstPlainTextVersion()
和FindFirstHtmlVersion()
。
The variable allMessages is not OpenPop but now it's List<MimeKit.MimeMessage>
type. 变量allMessages不是OpenPop,但现在是
List<MimeKit.MimeMessage>
类型。
lvnf
is a ListView control and when I click on an item it should show in RichTextBox I have the content of a message I clicked on. lvnf
是一个ListView控件,当我单击一个项目时,它应该显示在RichTextBox中,我具有单击的消息的内容。 But this two properites not exist in MailKit(MimeKit.MimeMessage)
. 但是,这两个
MailKit(MimeKit.MimeMessage)
在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();
}
}
What i tried so far is to create a new class HtmlPreviewVisitor
and added the class example from the mailkit site: 到目前为止,我尝试创建一个新的
HtmlPreviewVisitor
类,并从mailkit网站添加该类示例:
Then using it in form1
: 然后在
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;
}
And using the Render like this: 像这样使用渲染器:
void lvnf_Click(object sender, EventArgs e)
{
MimeKit.MimeMessage message = null;
var firstSelectedItem = ListViewCostumControl.lvnf.SelectedItems[0];
try
{
message = allMessages[firstSelectedItem.Index];
Render(message);
}
}
But the result in richTextBox1
is I see html content and not the text of the body. 但是
richTextBox1
的结果是我看到的是html内容,而不是正文。
For example: 例如:
<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>
...
You can use the TextBody
and/or HtmlBody
properties to get the equivalent of what you'd get with the methods you mentioned from OpenPop. 您可以使用
TextBody
和/或HtmlBody
属性来获得与从OpenPop中提到的方法等效的功能。
Like this: 像这样:
void Render (MimeKit.MimeMessage message)
{
var tmpDir = Path.Combine(Path.GetTempPath(), message.MessageId);
var visitor = new HtmlPreviewVisitor(tmpDir);
Directory.CreateDirectory(tmpDir);
message.Accept(visitor);
// Note: You will need to convert HTML into RTF
var rtf = ConvertHtmlToRtf (visitor.HtmlBody);
// Use the Rtf property instead of the Text property because
// the Text property does not support RTF formatting commands.
richTextBox1.Rtf = rtf;
}
Using ActiveUp this is the class that instantiate the connection and has the method to read 使用ActiveUp,这是实例化连接并具有读取方法的类
using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;
using ActiveUp.Net.Security;
using System.Diagnostics;
using System;
namespace servizioWiki
{
class readMail
{
public Imap4Client client = new Imap4Client();
public readMail(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
{
client.ConnectSsl(mailServer, port);
}
else
client.Connect(mailServer, port);
client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get { return client ?? (client = new Imap4Client()); }
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
try
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
catch(Exception ecc)
{
EventLog Log = new EventLog("Application");
Log.Source = "ServizioWiki";
Log.WriteEntry("Errore durante la lettura delle mail, dettagli: " + ecc.Message, EventLogEntryType.Warning);
return null;
}
}
}
On my service: 为我服务:
read = new readMail(
"some.mail.server",
port,
true,
this.username,
this.password
);
...
var emailList = read.GetAllMails(this.folderEmail);
Mailbox bbb = read.client.SelectMailbox(this.folderEmail);
int[] unseen = bbb.Search("UNSEEN");
foreach (Message email in emailList)
{
byte[] array0 = Encoding.ASCII.GetBytes(email.BodyText.Text);
EmbeddedObjectCollection immagini_mail = email.EmbeddedObjects;
MimePartCollection immagini_mail2 = email.UnknownDispositionMimeParts;
string HtmlEmail+= email.BodyHtml.Text.ToString();
string TextEmail= email.BodyText.Text.ToString();
if (immagini_mail.Count > 0)
{
//do something with images
}
}
Basically you can get the plain text or the html of the email and the attachments hope this helps, let me know 基本上,您可以获取电子邮件的纯文本或html以及附件,希望对您有所帮助,让我知道
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.