Advertisement

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.

Step 1: Create an Android Application

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.

Step 2: Creating the Intent

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);

Step 3: Configuring the Intent Type

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

Step 4: Launching 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

Step 5: Handling When Multiple Applications Can Send Mail

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.

Step 6: Supplying Message Content

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.com" };
String aEmailCCList[] = { "user3@fakehost.com","user4@fakehost.com"};
String 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.

Conclusion

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.

About the Authors

Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams TeachYourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to androidwirelessdev+mt@gmail.com, via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon

Advertisement