How to send an email with JavaMail API in Android ?

As an Android developer, you’re used to send email thanks to Android standard API provided in standard SDK. To achieve that, you use Intent.ACTION_SEND and call startActivity method to delegate to OS the sending email process.

Perhaps, you would like to manage the entire process in your application when you send an email in Android. If you want to make this, the solution is to use the port of JavaMail API for Android that is available here :

This port consists in 3 jars that you must download :

  • activation.jar
  • additionnal.jar
  • mail.jar

Once you got these jars, you can put them in the libs directory of your application project. Now, we can start the tutorial.

1. Define a JSSE Provider

First step is to define a JSSE (Java Security Socket Extension) Provider. To make that, we can get JSSE Provider of the Harmony project :

2. Implement a DataSource for data to send

To transport data during sending email, you must implement javax.activation.DataSource . Here, we choose to create a byte array implementation :

3. Create Mail Sender

Third step is to create email sender object that will contain all the logic to send email. Here, we’re going to use GMail as SMTP server. So, class will be named GMailSender :

In a static block, we add our JSSE Provider to Security class.

Like our email sender is specialized for GMail, mailhost is hard coded. So, constructor takes only user and password to authenticate to SMTP server. In constructor, we define all the Properties used during the Session which we get a default instance.

You need also to override getPasswordAuthentication method and you must return a PasswordAuthentication instance that use user / password entered in constructor.

Last step is the sendMail method that takes as arguments subject, body, sender and recipients. Here, we use MimeMessage associated to current Session. Email body is transported inside our ByteArrayDataSource previously created.

To send email, we finish by call static method Transport.send with message in parameter.

4. Send an email

To send an email, don’t forget to add Internet permission to your AndroidManifest :

<uses-permission android:name=”android.permission.INTERNET” />

It’s also important to send email in a separate Thread to avoid NetworkOnMainException. So, code is here :

5. Test the application

To test the application, you must authorize external access to your GMail account by going to this page :

Then, you must enable less secure apps :

Image for post
Image for post

Now, you can launch your Android application and check that GMailSender works pretty well :

Image for post
Image for post

To discover more tutorials on Android and Java development, don’t hesitate to have a look at my YouTube channel :

You can also have a look to the SSaurel’s Blog :

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store