I am trying to create a mail sending application in Android.
If I use:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
This will launch the built-in Android application; I'm trying to send the mail on button click directly without using this application.
Thank you for your valuable information. Code is working fine. I am able to add attachment also by adding following code.
here is an alt version that also works for me and has attachments (posted already above but complete version unlike the source link, which people posted they cant get it to work since its missing data)
and to call it in an activity...
Add this line in your manifest:
In case that you are demanded to keep the jar library as small as possible, you can include the SMTP/POP3/IMAP function separately to avoid the "too many methods in the dex" problem.
You can choose the wanted jar libraries from the javanet web page, for example, mailapi.jar + imap.jar can enable you to access icloud, hotmail mail server in IMAP protocol. (with the help of additional.jar and activation.jar)
Word of warning if using "smtp.gmail.com" as the default smtp server.
Google will force you to change your linked email account password frequently due to their over zealous "suspicious activity" polices. In essence it treats repeated smtp requests from different countries within a short time frame as "suspicious activity". As they assume you (the email account holder) can only be in one country at a time.
When google systems detect "suspicious activity" it will prevent further emails until you change the password. As you will have hard coded the password into the app you have to re-release the app each time this happens, not ideal. This happened 3 times in a week to me, I even stored the password on another server and dynamically fetched the password each time google forced me to change it.
So I recommend using one of the many free smtp providers instead of "smtp.gmail.com" to avoid this security problem. Use the same code but change "smtp.gmail.com" to your new smtp forwarding host.
Without user intervention, you can send as follows:
Send email from client apk. Here mail.jar, activation.jar is required to send java email. If these jars are added, it might increase the APK Size.
Alternatively, You can use a web-service at the server side code, which will use the same mail.jar and activation.jar to send email. You can call the web-service via asynctask and send email. Refer same link.
(But, you will need to know the credentials of the mail account)