views:

217

answers:

2

Hi, I am working on an IMAP client using java mail. We currently have a requirement of creating a "group by conversation" feature where user can view mails as conversation (Refer to how gmail groups mails that are replied and forwarded)

I am able to retrieve mails from the server, but in the mail parts i recieve, the replied mail is "fused" with actual mail and is treated like actual mail content.

eg: initial mail:

<div>This is the initial mail.</div>

replied mail:

This is the response Mail<br><br>
<div class="gmail_quote">
On Wed, Aug 26, 2009 at 4:26 PM, [censored] 
<span dir="ltr">&lt;
<a href="mailto:[censored]@[censored].com">
[censored]@[censored].com
</a>
&gt;
</span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
    <div>This is the initial mail.</div>
    <div></div>
</blockquote>
</div>
<br>

as shown above, there is no indication of what was actual and what was replied mail, how then is it possible to provide a grouping like gmail (regardless of server from where mail is read from)?

+7  A: 

If you get the raw source of the messages you can relate messages to one another by looking at the following headers:

Message-ID: <097819EBC7F79F4A850C8F088D35927302442A82AA@xxxxxxxxxxxxxxxxxxx>
References: <4A8BE8F3.2060007@xxxxxxxxxxxxxx>
In-Reply-To: <4A8BE8F3.2060007@xxxxxxxxxxxxxxx>
Kees de Kooter
I was looking at the same thing in my gmail box; I think this is the way it works since "sometimes" the conversation is broken.This is definitely the way I would use for this purpose.
Aif
This is not correct because only certain mail clients put In-Reply-To not all, the only best way is to do string containment condition.
Akash Kava
The IMAPMessage class indeed has the getInReplyTo() methodhttp://www.j2ee.me/products/javamail/javadocs/com/sun/mail/imap/IMAPMessage.html#getInReplyTo()Given that i have the IMAPFolder object, how do i retrieve the IMAPMessage Objects from it ?this:IMAPMessage[] messages = (IMAPMessage[])userFolder.getMessages();does this:got the exception: [Ljavax.mail.Message;java.lang.ClassCastException: [Ljavax.mail.Message;
Salvin Francis
Never mind, i am able to get the header, this is what i got in getInReplyTo(): <[email protected]>This does not even resemble the email id that replied the mail, am i missing something here ?
Salvin Francis
This is message id, you will need to do message.getHeader("Message-ID") to get it for all messages and compare the header value.
Akash Kava
A: 

Track By Subjects only

Most of Outlook and other email clients only group by the subject lines, for example they strip FW: RE: etc from subject's beginning and compare them without case sensitivity, and they mark them as same group. Mostly in reply/forward people dont change subject, and I doubt there is any other easiest way.

The other way to do this is (very expensive), you have to extract pure TEXT out of html content, and then for the same recepient/sender emails in last couple of days, you can find out if an old email's pure text content exists in any new email and then mark them as conversation.

getInReplyTo only returns message ID, you will have to manually load each message from each folder and find right Message ID, also you must cast your folder to IMAPFolder, did you do that?

Akash Kava