0

直到现在我都使用 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&amp;rtr=on&amp;s=x8pb08,2jot2,54g,7zdr,51ie,5vra,3kxd&amp;MLM_MID=4277846&amp;MLM_MLID=6640&amp;MLM_SITEID=2010001400&amp;MLM_UNIQUEID=4300628cb1">click here</A></td>
                    </tr>
                </tbody>

...
4

2 回答 2

2

您可以使用TextBodyand/orHtmlBody属性来获得与您在 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;
}
于 2016-05-18T20:12:21.230 回答
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 和附件希望这有帮助,让我知道

于 2016-05-19T09:40:24.753 回答