How to retrieve email body of any email, using Java Mail API - java

I am developing a Mail Client in Java (JSP and Struts). I have successfully fetched Message header information and stored in my local Database (including Message ID and subject).
When the user views the mails, I fetch the message headers from my database and display them to user. Now when the user click a particular Email, I want to fetch the mail body and file attachments from the Gmail server, directly. I don't want to serially travel through all the mails on the Gmail Server.
I have done this earlier in PHP, where if I pass the message id and I can retrieve the details of that particular mail. Is there any similar functionality in Java Mail API? If not, then can anyone suggest me a solution to this?

You can use Folder#getMessage(int) for this.
Note that the zip file with the JavaMail API which you can download from their side includes a lot of examples in /demo folder, under each a basic(!) Servlet which shows a simple mailbox with this functionality. You may want to build, refactor and expand further based on the simple example.

Related

how send email from android application directly?

I want the email to be sent immediately, currently the application is showing me installed email applications in device where I can recompose the email before sending, I want to send the mail directly without showing installed applications.
I Know javamail but need email .. other user can decompiler app and theft email :)
The way to go is to send the mail via some server, not directly from the device. You would, for example, connect to a REST API that sends out the email for you. Sending an email directly from the device from the user's email address is not (and should not be) possible
There are two ways to send email from android application directly without any intent
using SMTP & JavaMail API follow the link for details
using Webservice( for example PHP script ) so there is a server side code and you hit that URL with param like ( name,subject etc) so basically PHP code send mail at the end and it's very easy to use .
Personally, I suggest you use Webservice because it's simpler than first approach.
** Edit: You can save email in sharedpreferences or database and fetch it from there when you try to send mail rather than hardcoding it to avoid theft email risk.(directly putting mail address inside code)
Anyway you can prevent other from getting your full source code.See this answer

Save email attachment's link in android database downloaded by javamail

I want to store all emails read by javamail so that next time only recent emails requires to download.All email fields are look simple to save in database except attachments.The bodypart of attahment returns inputstream instead of filepath.
My question is how to save attachment links for each email so that it can be download only when user wants.
I think you can achieve this easier: If somebody wants to download the attachment just reconnect to the server and search for the given email, then download the attachment. So you don't need to store a link or anything in the database. Searching for a mail can be done by using SearchTerms an example for that can be found here.
Attachments aren't files or links unless you turn them into files or links.
If you're writing a web user interface to email, you'll generally want to leave all the message data in the email server and fetch it from there when needed. Depending on your user requirements, you might want to copy the messages from a "remote" email server to a "local" email server.
If you're determined to store all this information in a database, you're going to have to decide whether to store arbitrarily large attachments as BLOBs in the database, store them somewhere else (in the filesystem?), or leave them in the original email server and fetch them on demand.
Your web application will need to "create" a URL that represents each attachment, and when it gets a request for the URL it will need to interpret it and find the attachment wherever you decided to store it. There's probably 100 ways to do this, and probably 90 of them are insecure, so be careful.

Send mail (Java vs Php)

I have developed an app with a login. To help users I would like to send a mail reminding their credentials.. 2 solutions but 2 problems:
1) If I send the mail from the mobile phone (using my gmail account and mail.jar library) Google always reports a possible violation of my account or a suspect access, blocking it. I read several forum page about remove this feature but some people say that Gmail is built to be used by a single person
2) If I send a mail from a php page (using the function mail($mailto,$subject,$message,$header) several mail server put automatically this email into the trash folder.
Any suggestion? How to solve this issue?
Thank you in advance
You could send mail in java (mail.jar or mailServices of Spring Framework). I suggest you to use a mail provider service to send mail. It's better because there many things to care with when you send email ( email does not exist, delay of receiving the email, the current status of email sent, received or read...)

Best way to send an E-Mail from a java Application

I've been searching for the best way to send an e-mail with an attachment by my java application. I want to use this as an users bug report with logger files. The recipient should be my own e-mail address. I'd prefer the use of an e-mail client.
I tried the following:
Send an e-mail with user authentification like this. I don't want to use this, because the user would need to reveal his e-mail account and password. Furthermore, I'd have to set the properties for every e-mail adress, which is impossible.
Send an e-mail directly to my own e-mail address like this in Listing 16.16 (didnt found an english example). The problem is every e-mail server is using POP authentification nowadays, that means the recipient e-mail server won't accept my e-mail.
Using the mailto URL syntax like this. Doesn't work aswell, because the attachment function isn't working properly in every e-mail client. Best solution so far is to brief the user to add the attachment by himself, after I would put it to his desktop. Or upload the data and add a link to the e-mail body.
The last way I've found is this one. As you could assume this won't work either, because the localhost needs to be connected to the internet and capable enough to send an e-mail.
Hopefully I explained my problem well enough. Is there a different way to send bug reports?
The generally accepted way to get around the problems you describe is to keep all the email logic server side, and then have your application call a web service with the appropriate parameters. It's pretty easy to knock a PHP script / servlet up that will do the job and then send the results on via email, put them in a mysql database or so on.
However, if you can't / don't want to / won't keep this server side, I'd recommend using JavaMail to create a MimeMessage, then using writeTo() to write this to an EML file.
You can then do the usual:
Desktop.getDesktop().open(emlFile);
...which will open the EML file with the default application for handing those files, which is almost always the mail client. Still not foolproof, but if you're determined on sending the email from the client directly I think that's as good as you'll get.

Auto Generated Mail

Perhaps this kind of question comes under not to ask category but for the sake of application I have to ask.So pardon me.
At the time of Log in,I found that(suppose)If I am unable to remember my password,then I have to put my email-id then an auto generated reply come from that's hosted site with
a new link for generating another new password or
simply sending password(that means password isn't encrypted,I guess) in reply.
watching the source code it's hard to predict,but I want to know(if they are using jsp) then which protocol is used for this auto generated mail what are the other things to remember to achive this, while I'm also making an application for auto generated mail.
I have done this using javax.mail api in java
google for sending mail using java...
define content of mail in template or resource file and send mail whenever user clicks on forgot password link.
It is using SMTP protocol only.

Categories