Android Email App with GMail SMTP using JavaMail

Last modified on August 1st, 2014 by Joe.

Sending email using an Android app, which we custom build using with GMail SMTP using JavaMail API will be fun. With respect to using GMail’s SMTP server to send email is simple and easy to do. Code for the component of sending email in Java platform and Android app are same. Jars used from JavaMail API will be different for Java platform and Android, but the code we write will be same.

I already wrote a tutorial for sending email using Java and GMail SMTP with JavaMail API. Refer that for understanding about the core service of sending email. It has got couple of steps only and easy to understand.

JavaMailAPI with GMail

Here in this Android tutorial, we will reuse that same code which used to send email using GMail from Java. Remember we cannot use the same Jar files that we used from JavaMail API, but we need to use the ported version of JavaMail for Android. As far as GMail provides its SMTP server for our use, we can enjoy it for free like this, thanks to Google. We should have an email id created from GMail.

There three important source files that needs to be discussed in this example Android app for sending Email using GMail,

1. – GMail Email Sender using JavaMail

This Java class is not specific to Android and can also be used in general Java SE platform by using the general JavaMail API jars.


import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.util.Log;

public class GMail {

	final String emailPort = "587";// gmail's smtp port
	final String smtpAuth = "true";
	final String starttls = "true";
	final String emailHost = "";

	String fromEmail;
	String fromPassword;
	List toEmailList;
	String emailSubject;
	String emailBody;

	Properties emailProperties;
	Session mailSession;
	MimeMessage emailMessage;

	public GMail() {


	public GMail(String fromEmail, String fromPassword,
			List toEmailList, String emailSubject, String emailBody) {
		this.fromEmail = fromEmail;
		this.fromPassword = fromPassword;
		this.toEmailList = toEmailList;
		this.emailSubject = emailSubject;
		this.emailBody = emailBody;

		emailProperties = System.getProperties();
		emailProperties.put("mail.smtp.port", emailPort);
		emailProperties.put("mail.smtp.auth", smtpAuth);
		emailProperties.put("mail.smtp.starttls.enable", starttls);
		Log.i("GMail", "Mail server properties set.");

	public MimeMessage createEmailMessage() throws AddressException,
			MessagingException, UnsupportedEncodingException {

		mailSession = Session.getDefaultInstance(emailProperties, null);
		emailMessage = new MimeMessage(mailSession);

		emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
		for (String toEmail : toEmailList) {
			Log.i("GMail","toEmail: "+toEmail);
					new InternetAddress(toEmail));

		emailMessage.setContent(emailBody, "text/html");// for a html email
		// emailMessage.setText(emailBody);// for a text email
		Log.i("GMail", "Email Message created.");
		return emailMessage;

	public void sendEmail() throws AddressException, MessagingException {

		Transport transport = mailSession.getTransport("smtp");
		transport.connect(emailHost, fromEmail, fromPassword);
		Log.i("GMail","allrecipients: "+emailMessage.getAllRecipients());
		transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
		Log.i("GMail", "Email sent successfully.");


2. – Android Activity to Compose and Send Email

In our example, this is the default Android Activity for the Android app. This is the email compose screen, when we start this example app we will just directly land in the email compose page. It will have the basic email fields like, from, to, subject, body. We can give the from email address and its respective password as input argument. This example app uses above class to send email which comes with default settings that is configured for sending email with GMail SMTP. So if you want to use any other SMTP server, then we have to update the smtp settings there.


import java.util.Arrays;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SendMailActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		final Button send = (Button) this.findViewById(;

		send.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Log.i("SendMailActivity", "Send Button Clicked.");

				String fromEmail = ((TextView) findViewById(
				String fromPassword = ((TextView) findViewById(
				String toEmails = ((TextView) findViewById(
				List toEmailList = Arrays.asList(toEmails
				Log.i("SendMailActivity", "To List: " + toEmailList);
				String emailSubject = ((TextView) findViewById(
				String emailBody = ((TextView) findViewById(
				new SendMailTask(SendMailActivity.this).execute(fromEmail,
						fromPassword, toEmailList, emailSubject, emailBody);

3. – Android AsyncTask for Sending Email

I have used AsyncTask, as sending email is best done in a separate thread. SendMailActivity will receive the user email arguments and pass it on to the SendMailTask to send email. SendMailTask interacts with by passing parameters to and publishing mail sending status using ProgressDialog.


import java.util.List;

import android.os.AsyncTask;
import android.util.Log;

public class SendMailTask extends AsyncTask {

	private ProgressDialog statusDialog;
	private Activity sendMailActivity;

	public SendMailTask(Activity activity) {
		sendMailActivity = activity;


	protected void onPreExecute() {
		statusDialog = new ProgressDialog(sendMailActivity);
		statusDialog.setMessage("Getting ready...");

	protected Object doInBackground(Object... args) {
		try {
			Log.i("SendMailTask", "About to instantiate GMail...");
			publishProgress("Processing input....");
			GMail androidEmail = new GMail(args[0].toString(),
					args[1].toString(), (List) args[2], args[3].toString(),
			publishProgress("Preparing mail message....");
			publishProgress("Sending email....");
			publishProgress("Email Sent.");
			Log.i("SendMailTask", "Mail Sent.");
		} catch (Exception e) {
			Log.e("SendMailTask", e.getMessage(), e);
		return null;

	public void onProgressUpdate(Object... values) {


	public void onPostExecute(Object result) {


4. AndroidManifest.xml

Android Manifest has got the usual configuration and we need to add an extra line. This is for giving permission for our Android app to access internet to send email using GMail.

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

Output: Android Email App Activity


Output: Android ProcessDialog Sending Email


Add Dependency Jar to Android

I have not provided the dependent JavaMail jars with the project source code download below. You need to download jars mail.jar, activation.jar and additional.jar from JavaMail-android download page.

Download Example Android App Project to Send Email with GMail using JavaMail

Comments on "Android Email App with GMail SMTP using JavaMail"

  1. Amit says:

    Fantastic. thanks a lot.

  2. Sandeep says:

    Are you publishing android apps. I hope if you do, they will be great.

  3. Shajeel Afzal says:

    Thanks for this tutorial but i am confused about the Password thing. Is it actual gmail account password?

  4. phani says:

    thanq you joe sir……………..

  5. phani says:

    fine sir nice artical can u pls tell me

    if a runtime exception is thrown in the finalize method The exception is simply ignored and the object is garbage collected.

    why so this behaviour?? (waiting for replay)

  6. Nirmalrevar says:

    It was so helpFul

  7. Pratik says:


  8. sashanka says:


    Thanx for the code.But I am getting Force Close whan I click on Sendmail.Please give me suggestion to solve the problem.

  9. toni says:

    Thanks Joe, this works perfectly for my app.

  10. Kaustubh says:

    Hi Joe, this is a fantastic tutorial. But I am having an issue. similar to this one.
    I have an app which shows map and button on it. I want to notify the user’s current location to another mobile, when I click on that button. (just like facebook chat. when One friend sends message to second friend, second friend gets notified)
    How do I do that? Please help!

  11. David Zondray says:

    Very informative; Thanks for sharing.

  12. Ali says:

    How we can use UTF8 for sending other language?
    Thanks for your answer.

  13. […] far we have seen how we can send email in Java using JavaMail API and then an email app in Android using JavaMail API and GMail SMTP. Now to complete this email series (at least for now), we shall do email with Spring and […]

  14. Vikash says:

    Having warning in
    String emailBody = ((TextView) findViewById(
    new SendMailTask(SendMailActivity.this).execute(fromEmail,
    fromPassword, toEmailList, emailSubject, emailBody);

    Last two lines showing error. and not working .

  15. P.Sritharan says:

    thanks a lot sir

  16. smitha says:

    Having warning in
    String emailBody = ((TextView) findViewById(
    new SendMailTask(SendMailActivity.this).execute(fromEmail,
    fromPassword, toEmailList, emailSubject, emailBody);

    One more problem..
    I am getting Force Close whan I click on Sendmail.Please give me suggestion to solve the problem.

  17. smitha says:

    plzzzzzzzzzzzzzzz urgent

  18. Jose Daniel says:

    Hi Joe:

    Thanks for this tutorial is very useful. I have a question, if you could answer I would really aprecite it.

    I follow this tutorial and create an application, but the problem is that it works normally in my Android 2.3 cell. But in my Lenovo A3300 Android 4.2.2 tablet don’t send the message LogCat says that cannot be connected to smtp 587 port.

    Please a I need any respond

    Best Regards

  19. Joe says:


    Need more information to resolve this. Check for information in LogCat, we should find out why its not able to connect to smtp port.

  20. hossein says:

    plz help me

    01-11 13:48:59.818: E/MailApp(29958): Could not send email
    01-11 13:48:59.818: E/MailApp(29958): android.os.NetworkOnMainThreadException
    01-11 13:48:59.818: E/MailApp(29958): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(
    01-11 13:48:59.818: E/MailApp(29958): at
    01-11 13:48:59.818: E/MailApp(29958): at
    01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.InternetAddress.getLocalAddress(
    01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(
    01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.updateMessageID(
    01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.updateHeaders(
    01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.saveChanges(
    01-11 13:48:59.818: E/MailApp(29958): at javax.mail.Transport.send(
    01-11 13:48:59.818: E/MailApp(29958): at com.fahadalawam.kwtresturant.Mail.send(
    01-11 13:48:59.818: E/MailApp(29958): at com.fahadalawam.kwtresturant.Add$1.onClick(
    01-11 13:48:59.818: E/MailApp(29958): at android.view.View.performClick(
    01-11 13:48:59.818: E/MailApp(29958): at android.view.View$
    01-11 13:48:59.818: E/MailApp(29958): at android.os.Handler.handleCallback(
    01-11 13:48:59.818: E/MailApp(29958): at android.os.Handler.dispatchMessage(
    01-11 13:48:59.818: E/MailApp(29958): at android.os.Looper.loop(
    01-11 13:48:59.818: E/MailApp(29958): at
    01-11 13:48:59.818: E/MailApp(29958): at java.lang.reflect.Method.invokeNative(Native Method)
    01-11 13:48:59.818: E/MailApp(29958): at java.lang.reflect.Method.invoke(
    01-11 13:48:59.818: E/MailApp(29958): at$
    01-11 13:48:59.818: E/MailApp(29958): at
    01-11 13:48:59.818: E/MailApp(29958): at dalvik.system.NativeStart.main(Native Method)

  21. tulsi says:

    it gives error “Unfortunately app has stopped” cant solve help!!! asap

  22. Joe says:


    thanks for a very nice tutorial. I have a question though.

    Why do we need to use the javamail-android port? Are there some problems with the standard javamail implementation on Android?

    My only concern is that the last version is from September 2009.

    Thanks for your help

  23. Rajasekhar says:

    Example is not working please send me working zip file
    when i run the error is coming
    like11-19 12:14:39.835: E/AndroidRuntime(18910): at

  24. amir lavi says:

    thank you very much very very usefull sample

  25. shah says:

    sir how to receive mails in android
    i search every where but am unable to get code to receive mail code plz help me

  26. Ankit says:

    thank you… Its working..

  27. Alejandro says:

    can u help me plz if i want to send image what i need to add in this code…

    sorry my bad english i speck spanish

  28. akhsit says:

    akshit is here very nice tutorial

  29. zk says:

    could you help me to add attachment field to this code to let user send an attachment

  30. mahesh says:

    Thanks A lot You save me ..

Comments are closed for "Android Email App with GMail SMTP using JavaMail".