Quick Tip: Enabling Users to Send Email From Your Android Applications – The Easy Way

This quick tip shows you how to launch the built-in Mail application, supply it with data, and allow the user to send an email message. You will achieve this by creating and configuring the appropriate Intent within an application’s Activity.
Begin by creating an Android project. Implement your Android application as normal. Once you have a project set up and the application running, decide under what circumstances you want to launch an email application and send an email. Will this occur when a Button control is pressed? Do you want to fill in the details of the email, or simply allow the user to do so? Implement the necessary Button or menu control, including any click handling. Once you have completed this task, you have a place to drop in the code to launch the mail client.
If you are using an emulator, you’ll need to configure the email client. If the email client is not configured, it will not respond to the Intent we’ll be discussing. If you want to see the chooser in action, you’ll need to configure a device using multiple messaging applications, such as the Gmail application and the Email application.
Now you are ready to proceed with this quick tip.
You can also get started with the open source code provided for this quick tip.
Mail applications can be launched to send a message with the following Intent: android.content.Intent.ACTION_SEND. Begin by creating an Intent of this type, as follows, within your Button click handler:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
This is a very generic Intent type. Basically, it tells the Android system that your application wants to send some sort of message somewhere—the ACTION_SEND Intent can be used to send different types of messages including email messages, text messages (SMS), picture messages (MMS), etc. Therefore, you need to add some additional configuration information to the Intent in order for the Android operating system to properly match your request to the appropriate application.
For example, if you’re planning to send text data, you would set the type of the Intent using the setType() method as follows:
emailIntent.setType("plain/text");
On a typical device, the Messaging application and the Mail application generally handle the sending of plain text messages.
Configuring The Intent
When you launch this Intent, any applications that support plaintext messaging will be able to handle this request. Once you have set the type data for the Intent, you can call the startActivity() method, passing in your Intent:
startActivity(emailIntent);
Launching the Intent
Users can install any number of Android applications on their devices. Often, they install multiple messaging applications. For example, they might have separate personal email client and work email client applications. You can allow the user to choose which application to handle the Intent by supplying the startActivity() call with createChooser() method:
startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
This will prompt the user will all applications that have an intent filter set up that matches your plain text ACTION_SEND request. The user can choose a specific application to launch.
Handling When Multiple Applications Can Send Mail
Note: If there is exactly one choice available, that choice will be launched directly. If there are zero choices available, the dialog shows but tells the user there are no choices. If there are two or more choices, the user is given the choice while not given the option to make a default choice since the chooser is being forced.
Often, you want to supply some of the email message fields with data in order to save the user some typing. For example, you might want to set the email recipient list, the message subject, or the message body. You can do this by supplying this information as part of the Intent’s extras.
The following code creates the Intent, configures the extras, and launches the new Activity:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

String aEmailList[] = { "user@fakehost.com","user2@fakehost.comg aEmailCCList[] = { "user3@fakehost.com","user4@fakehost.com aEmailBCCList[] = { "user5@fakehost.com" };

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
emailIntent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList);
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList);

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject");

emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "My message body.");

startActivity(emailIntent);
Now, if you launch the ACTION_SEND Intent, the fields are filled in for the user.
Supplying Message Content
A Note on Permissions: Although your application is leveraging messaging capabilities on the device, it is not required to have any such permissions. This is because the application is not directly sending messages itself. Instead, it’s just leveraging other applications’ capabilities to do so.
In this quick tip you have learned how to configure an Intent to launch and send an email message. The user still has ultimate control on whether or not to send the message. However, this feature can be very useful for applications wishing to include simple user feedback functionality or to integrate tightly with the user’s preferred email client.

Comments

Popular posts from this blog

How to Create a Yoga Goddess Illustration in Adobe Illustrator

How to Create an Icon Set using Adobe Photoshop

Android Essentials: Using the Contact Picker