Android SDK: How To Send Email Without User Interaction Using SMTP
December 29, 2011
This tutorial will show you how to make an app to send an email without any user interaction, that is we will not make use of intents to use an existing app with pre-filled information but instead we will use SMTP, you will also be able to easily attach files by simply specifying the path and file name.
inbox showing email sent from app
External Libraries: JavaMail
Unfortunately though Android doesn’t support sending email like this out of the box, fortunately there is a Java class called JavaMail that does and there is even an Android port for it. Head over this website and download all three files: additional.jar, mail.jar and activation.jar . Put these files in a folder called lib inside your project folder as shown in the picture below.
location of JavaMail jar files as seen in eclipse's package explorer
Now that you have added the files to the lib folder, for each one: right click->Build Path->Add to Build Path
Mail Wrapper Class
Although the jar files you just added to your project provide the functions we need to send SMTP email they still need to be configured, fortunately for us someone already wrote a class to interface these jar files and all we have to do is add this class to our project into the src folder.
Make a file in your src folder called Mail.java with the following code, note that this is where you define define your SMTP domain and port. Gmail is used in this case:
Our interface will be just one button which upon clicking will attach a file and send the email to the specified address with the specified subject and body content.
the app's interface, click the button to send the email
Android Mail Activity
This activity is pretty short and self explanatory. The file am attaching is a picture called myPhoto.jpg located in my SD card.
If everything goes as expected you’ll get this message, thanks to the toasts, after clicking the send email button:
Lastly here is a screenshot of my package explorer just in case you don’t know where the files go.
location of all files presented in this tutorial
What are you planning to do with this code? let us know in the comments below.