0 votes

I send an Email to someone and got a reply of that Email. So now I want to extract only the Replied text using Mail.dll library. How can I do that? When I use

var eml = imap.GetMessageByUID(uids.Max());
email = new MailBuilder().CreateFromEml(eml);

email.Text; or email.GetBodyAsHtml();

I get New replied content but old text also.

by (850 points)

1 Answer

0 votes

Unfortunately as there is no standard for that and it's almost impossible to do that reliably without seeing the whole conversation (analyzing emails with corresponding ids in IMail.References collection).

I have a small class that can parse out > replies from text emails:

internal class ReplyText
{
   public string Extract(string plainText)
   {
       List<string> lines = ReadLines(plainText);
       List<string> result = new List<string>();

       lines.Reverse();

       bool ingore = true;
       foreach (string line in lines)
       {
           if (line.StartsWith(">") == false)
               ingore = false;

           if (ingore == false)
               result.Add(line);
       }

       result.Reverse();
       return string.Join("\r\n", result.ToArray());
   }

   public List<string> ReadLines(string text)
   {
       List<string> lines = new List<string>();

       if (text == null)
           return lines;

       string line;
       StringReader reader = new StringReader(text);
       while ((line = reader.ReadLine()) != null)
       {
           lines.Add(line);
       }
       return lines;
   }
};

And tests:

[TestFixture]
public class ReplyTextTest
{
    [Test]
    public void Extract_NoReply_AllText()
    {
        const string text = @"there is only new text";
        Assert.AreEqual("there is only new text", new ReplyText().Extract(text));
    }

    [Test]
    public void Extract_OnDateSomeoneWrote()
    {
        const string text = @"Hi,
 Some text
 On Mon, Dec 19, 2011 at 8:07 AM,  <alice@example.com> wrote:
 > Hello
 >
 ";
        Assert.AreEqual(@"Hi,
 Some text
 On Mon, Dec 19, 2011 at 8:07 AM,  <alice@example.com> wrote:", new ReplyText().Extract(text));
    }

    [Test]
    public void Extract_MoreThen()
    {
        const string text = @"Some text
 > Hello";
        Assert.AreEqual("Some text", new ReplyText().Extract(text));
    }

    [Test]
    public void Extract_MoreThenInterlaced_NotExtracted()
    {
        const string text = @"Hi
 > Is this a Question?
 My answer is yes.";
        Assert.AreEqual(text, new ReplyText().Extract(text));
    }
};

But for HTML emails there's simply no way to do this without analyzing previous emails, or relying on client using blockquote...

by (301k points)
...