0

I used OpenPop until now but now change to MailKit and this two properties: FindFirstPlainTextVersion() and FindFirstHtmlVersion() not exist with MailKit.

The variable allMessages is not OpenPop but now it's List<MimeKit.MimeMessage> type.

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. But this two properites not exist in 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:

mailkit

Then using it in 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.

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&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>

...
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Daniel Lipman
  • 41
  • 1
  • 8

2 Answers2

2

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.

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;
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • I tried to use the example in the MailKit site the HtmlPreviewVisitor class. I created new class in my project and added the HtmlPreviewVisitor and then using it in form1 like they show. – Daniel Lipman May 18 '16 at 21:05
  • jstedfast i also updated my question now with what i tried and what i'm getting as result. What am i missing ? – Daniel Lipman May 18 '16 at 21:08
  • You should not use `HtmlBody` *and* `HtmlPreviewVisitor`, you need to choose to use one or the other. Also your code isn't even using the results from `HtmlPreviewVisitor` at all. Secondly, RichTextBox is meant to display RichText formatted text, not HTML formatted text which is why it looks like raw HTML. – jstedfast May 19 '16 at 11:35
  • Disregard my last comment except for the fact that RichTextBox does not know how to render HTML so you are using the wrong control. If you want an actual example of code that renders HTML, take a look at the sample program included with MimeKit: https://github.com/jstedfast/MimeKit/blob/master/samples/MessageReader/MessageReader/MessageViewWindow.cs – jstedfast May 19 '16 at 11:54
  • @jstedfast i cannot get an inline image that is having src url and not as base 64 or content type image. Can you point me out to any example how i can do it. – coder771 Aug 16 '16 at 10:37
  • You'd have to show me what the raw email message looks like so I know what you are talking about. – jstedfast Aug 16 '16 at 13:29
0

Using ActiveUp this is the class that instantiate the connection and has the method to read

 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

Lorenzo
  • 673
  • 1
  • 11
  • 25
  • What is the contento variable ? contenuto += email.BodyHtml.Text.ToString(); i can't figure out how to declare it what namespace. – Daniel Lipman May 19 '16 at 11:19
  • see my edits, it is a string, basically email.BodyHtml.Text gives you the html of the email and the ToString() returns it as a string – Lorenzo May 19 '16 at 12:25