How to parse emails and extract header and body part - java

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.

Related

javamail - preserve format when replying to multi-part messages

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.

How get the actual emailbody of email sent as riche text using PidTagRtfCompressed Canonical Property

I am trying to get the emailbody of the email, using ews java 2.0, sent in rich text format.
Though I am getting the text format correctly, but i am not receiving the html format of the same.
All I need is the exact representation of the emailbody in which the email was sent. So that I could reproduce the same in another system which requires html.
Is it possible for me to get it?
I tried using the PidTagRtfCompressed Canonical Property. I defined an extended property using the extended property definition with the corresponding tag(0x1009) and mapitype(binary) for the rtf emailbody. Though i did receive value in it but I am not able decode it to the corresponding readable value.
Would it be possible to get the readable value out of this extended property? Would it be possible to get the html format as well?
I would be grateful for any help.
Thanks
I am trying to get the emailbody of the email, using ews java 2.0, sent in rich text format.
If you just request the body using a GetItem operation EWS will return HTML, if the message has a Native RTF body the Exchange Store will do an on the fly conversion of the RTF message into HTML (the meta information in the HTML returned will tell you if a conversion has been done). You can test this without writing any code using the EWSEditor https://ewseditor.codeplex.com/ . Is there any reason your not using the Strongly typed properties as this would be the normal method of getting the body the rest of your question doesn't make much sense unless you have a problem with the Store conversion ?
Though i did receive value in it but I am not able decode it to the corresponding readable value.
If you want to that then you need to implement your own decompression library to do this eg something like https://github.com/Sicos1977/MSGReader/blob/master/MsgReader/Outlook/RtfDecompressor.cs should work. The protocol spec for this is https://msdn.microsoft.com/en-us/library/cc463890(v=exchg.80).aspx . Exchange has a library to do that but its limited in use to the server only as it mostly for use in Transport agents https://msdn.microsoft.com/en-us/library/microsoft.exchange.data.textconverters(v=exchg.150).aspx

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.

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