Home Tutorials Hints & Tips

Getting Started in Android Development

Part 23 - Sending Email

In this tutorial we will look at how to compose an email within a dedicated layout and use a pre installed email client to then send that email. It is important to note that the email client installed on the phone or tablet has to have an active and working email account.

For this tutorial a basic layout was constructed with EditText fields for the email address of the recipient, the subject and the message. A screenshot of this layout is shown below and the XML code follows.

The android basic layout for sending email

The job of the email Activity is to collect these key parameters and send them to the email client. After which the user can send the email. As with sending data between any Activities, we use bundles to “package up” that data. For this example a bundle of data being sent to the email client.  After setting up the variables, a private method called sendEmail() is created. The onClick method for the “compose email” button simply calls this method.

The first task in the snedEmail() method is to retrieve the text entered in the EditText fields using the variables below:

String to = emailAddressText.getText().toString();
String subject = subjectText.getText().toString();
String message = messageText.getText().toString();

A string array is then defined that will contain all email addresses that the email is to be sent to. Clearly, this needs to be an array as there may be more than one recipient. The definition of the string array is below:

String[] TO = {to};

You can setup similar array for any recipient that is to be copied or blind copied. For example:

String[] CC = {cc1, cc2, cc3} or String [] BCC = {bcc1, bcc2, bcc3}

As when we call any new Activity from with any Activity we need to setup and Intent. The same applies when we call the email client. Therefore, we need to setup the Intent within our email Activity.

Intent emailIntent = new Intent(Intent.ACTION_SEND);

The two lines below specify the message format and by adding just mailto: to the setData field we are informing the client that the user will specify the email address.

emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain")

The next task is the bundle of extra data that needs to be send to the email client. In our example, the bundle is the recipients email address, subject and message. There are other extras that are worth mentioning below:

The next three lines of code create our bundle:

emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);

We can pass the entire “TO” string array simply by referencing “TO”. No need for brackets.

We can then send the email by starting the Activity. The code below will prompt the user to select any installed email clients with working email accounts.

startActivity(Intent.createChooser(emailIntent, "send mail..."));

We need to surround this statement with a try and catch so that we can gracefully managed any situation where there is no email client installed.

After this tutorial your EmailActivity.java file should look similar to the one below:

After this tutorial your Android Manifest file should look similar to the one below:

After this tutorial your ListviewActivity.java file should look similar to the one below:


Download Download tutorial set 1

© 2015 North Border Software All rights reserved
Disclaimer Copyright