我使用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>
...
答案 0 :(得分:0)
您可以使用TextBody
和/或HtmlBody
属性来获得与您在OpenPop中提到的方法相同的内容。
像这样:
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;
}
答案 1 :(得分:0)
使用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;
}
}
}
在我的服务上:
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
}
}
基本上你可以获得电子邮件和附件的纯文本或html 希望这有帮助,让我知道