javamail - preserve format when replying to multi-part messages - java

When using Javamail API to iterate through messages, uncertain how to deal with multiple body parts. When I reply to it I would like for the reply to look be formatted as the incoming message.

First, you need to separate the main body of the message from the attachments. See the JavaMail FAQ to get started. This will give you the plain text and/or html text of the message.
Next, you need to decide how you're going to edit the original message to include the text from the reply. JavaMail doesn't help you with this. Are you going to display the message to a user or are you going to edit the text programmatically? Either way, this is likely to be the most difficult part unless you only deal with plain text messages.
Finally, with the new text, you can use the JavaMail Message.reply method to create the reply message and then set the content of the message using the edited text for the reply. Note that it's more complicated if you want to support multipart/alternative messages with both a plain text and html part, and even more complicated if the html part is part of a multipart/related that includes images that it refers to. An appropriate search will turn up many examples.
That's just a brief sketch of what's involved. If you have more specific questions, show us your code.

Related

javax.mail separating email threads

I have a java application that monitors an inbox and reads new messages. I only want the latest message in a thread read, however when an email with multiple replies in the same thread is parsed, it reads the whole thing.
Is it possible to read only the latest reply in an email thread using javax.mail? Or would I need to place some logic to look at the header and determine the latest by comparing the send date?
If you have separate messages in your mailbox for each reply, you have to decide how to determine that they're part of the same "thread". There's no perfect way to do this and different mailers will do it differently. A good start is the References and In-Reply-To headers. Once you know the set of messages that are part of a single thread, you can choose the latest one by date.
If you have a single message that includes the text of previous replies in the body of the message and you want to separate the latest reply from the previous replies, you'll have to process the text in the body and decide which parts are previous replies and which part is the current reply. Again, there is no perfect solution and this will require more heuristics.

notification if no reply to a mail in Gmail (Java)

I would like to know if it is possible to know if a recipient has answered a definit email .
What is te best way to do this in Java ?
There are Message-ID, In-Reply-To and References headers in the e-mail message (see http://cr.yp.to/immhf/thread.html or http://wesmorgan.blogspot.ch/2012/07/understanding-email-headers-part-ii.html). You have to keep the Message-ID of an e-mail message you are interested in, and then parse the headers of the incoming messages, if they contain your ID.
As for the way to do it in Java, read the Java Mail API Tutorial and study the javax.mail JavaDoc (and sub-packages).
Warning: Athough commonly used by most e-mail clients, these headers are not mandatory, so there is no 100% secure way to do it.
Just to be sure we understand the problem, you want to know how to tell if a recipient has sent a reply to you for a message you sent to the recipient, right?
As Jozef describes, the In-Reply-To and References headers are the standard way of doing this, but some mailers don't properly include them in replies. Another approach is to include a unique ID in the Subject of the original message. Replies almost always include the original Subject without change, prepended by "Re:" or equivalent.

How to parse emails and extract header and body part

I have a email file, if I open it as a text file, I see lots of nuisance. And if I open it in a mail program such as Kmail I can see what I can say as a simplified email, with subject, from, to, date and body.
I want to do the same in Java. Currently I'm reading the email file straight away and thus there are lot's of nuisance (HTML, etc) along with header fields and body.
Further I also want to check if a message is Base64 encoded or not!
Please guide me for these two things!
Use this MimeMessage constructor. See the JavaMail FAQ for the other things you'll need to know.

Parsing reply email to take only the latest part

In java mail, when i parse an replied email, how can I take only the latest message (the reply) and ignore the old ones?
I think you're asking about parsing the text content of the message to ignore the parts that are just previous messages that have been included in the response.
There's no well-defined way to do this. You're going to need to apply some heuristics to try to guess which parts of the text are these "quoted" messages. A common convention is that these messages are lines that start with ">", but that's not universal nor guaranteed.
I solved this problem by removing the <backquote> </backquote> parts of the original email if it was received as an HTML email, and by removing lines beginning with > on text email.

Web based email reply/quoting code available?

I'd like to write a sort of mini-CRM system that will require interacting with emails from customers (many different systems). I'd like for it to be able to reply to their emails, and place the original email in a nice quoted reply format like other email apps.
This appears to be fairly easy when responding to an ASCII email, but how do we format an incoming HTML as quoted (ie, with the little bar down the left side to indicate it is quoted text)? Is there code already available to do this (preferably in Java)?
Or perhaps I am overthinking the problem...
Similar, but not quite the same question
The best solution in most cases is to convent the HTML email to text email (most emails are sent as both, so you can also just "prefer" text).
That said, if you must send the email as HTML, use the <blockquote> tag.
Just add the <blockquote> tag around the quoted piece of text. Maybe you can use a little CSS to style the quoted text, but I'm not sure of that works in all mail clients.

Categories