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 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:

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.

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:

EXTRA_BCC: email addresses for blind carbon copy

EXTRA_CC: email addresses for carbon copy

EXTRA_EMAIL: email addresses for recipients

EXTRA_HTML_TEXT: supply an alternative to EXTRA_TEXT as HTML formatted text

EXTRA_STREAM: URI holding a stream of data supplying the data that are sent

EXTRA_SUBJECT: the subject of an email

EXTRA_TEXT: the message body of the email

EXTRA_TITLE: the title that is shown when the user has to choose an email client