Web based email reply/quoting code available? - java

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.

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

Is it safe to use email encoders? Or how is it safest to show email address?

Services like : http://www.wbwip.com/wbw/emailencoder.html encodes email to ASCII. Is it totally safe? Can spammers copy this code and decode it?
They can decode it, so how is it safest to show your email on the website? Probably by putting it in the image?
No, it is not safe at all. You are still exposing your email address on the page. Some dumb spiders will end confused, but those which are up to date will definitely be able to "decode" these entities quickly. The only solution is to... not expose email address at all. If you use contact form with recipients to be chosen by users, use IDs and dereference it in your code. If you need to show email - show image, but if anyone would like to have you address in their spam DB, then he can always put it by hand there.
Short answer - no, it is not fully secure as it can easily be decoded by anyone.
Anyone could easily decode this using an ASCII table, like so: http://www.asciitable.com/
A clever spam bot can trivially read the "hidden" address.
What you should do instead, is have something like (for john.doe#gmail.com):
john (dot) doe (at) gmail (dot) com
One could still construct a bot to counter this, but it would be impractical as this technique isn't very common.
Truth be told, there's no 100% way of hidhing your email. Even images have OCRs, and in the worst case, a persistent human can always manually type your address and add it to his to-spam list.
If you want your users to contact you, the best way would be to make a form, and have the server to process the request and send you an email. This way, your address is not exposed to anyone.
The best solution is not to show the email address at all. If you want your users to be able to send emails to each other, write a dedicated UI where they can type the message in and send the email directly from your server. But make sure that the UI itself is secured with login/pass too, otherwise you're back to square one.
That method would be safe against some - but not all spiders. If even one got through, you could be introducing the user to thousands of emails of spam. I guess it's up to you whether it's worth the risk, but I definitely wouldn't.
Sure, this form of email encoding is very weak in terms of security, in fact, it is not design for security purposes at all. It's encoding, not encrypting.

How do I send a query to a website and parse the results?

I want to do some development in Java. I'd like to be able to access a website, say for example
www.chipotle.com
On the top right, they have a place where you can enter in your zip code and it will give you all of the nearest locations. The program will just have an empty box for user input for their zip code, and it will query the actual chipotle server to retrieve the nearest locations. How do I do that, and also how is the data I receive stored?
This will probably be a followup question as to what methods I should use to parse the data.
Thanks!
First you need to know the parameters needed to execute the query and the URL which these parameters should be submitted to (the action attribute of the form). With that, your application will have to do an HTTP request to the URL, with your own parameters (possibly only the zip code). Finally parse the answer.
This can be done with standard Java API classes, but it won't be very robust. A better solution would be HttpClient. Here are some examples.
This will probably be a followup question as to what methods I should use to parse the data.
It very much depends on what the website actually returns.
If it returns static HTML, use an regular (strict) or permissive HTML parser should be used.
If it returns dynamic HTML (i.e. HTML with embedded Javascript) you may need to use something that evaluates the Javascript as part of the content extraction process.
There may also be a web API designed for programs (like yours) to use. Such an API would typically return the results as XML or JSON so that you don't have to scrape the results out of an HTML document.
Before you go any further you should check the Terms of Service for the site. Do they say anything about what you are proposing to do?
A lot of sites DO NOT WANT people to scrape their content or provide wrappers for their services. For instance, if they get income from ads shown on their site, what you are proposing to do could result in a diversion of visitors to their site and a resulting loss of potential or actual income.
If you don't respect a website's ToS, you could be on the receiving end of lawyers letters ... or worse. In addition, they could already be using technical means to make life difficult for people to scrape their service.

Categories