Advertisement
Android SDK

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

by

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

Related Posts
  • Code
    Android SDK
    Common Entry Points for Android Applications5d7z4 preview image@2x
    In this tutorial, we will provide an overview of common entry points for Android applications. These entry points will focus on ensuring that your application maximizes its utility to the user.Read More…
  • Code
    Android SDK
    Using New Relic to Monitor Your Android AppGetting started new relic retina preview2
    In the last two years, New Relic has focused hard on building out a solution for monitoring the performance of mobile apps. In this tutorial, we will look at how you can start using New Relic to monitor the performance of an Android application.Read More…
  • Code
    Creative Coding
    Using WordPress for Web Application Development: Features: EmailApplication foundation 400
    One of the major components of every web application is its ability to send email. Now this doesn't mean that each web application is its own mail client (though people do certainly create their own). Instead, I mean that applications normally send emails when a user signs up, a user modifies their password, when something in the system changes, or - more generally speaking - whenever something occurs for which the site owners want to notify the users.Read More…
  • Code
    Android SDK
    Android SDK: Next StepsAndroid preview@2x
    In this series, we've begun learning how to develop Android applications from scratch. We started exploring the development tools, got acquainted with the basic elements in an application project, looked at user interface design, interactivity, resources, and data, and we've also took a closer look at what happens when your application is running. What we've covered so far should put you in a good position to get started creating functional Android applications, but Android has a lot more to offer so the range of possibilities is virtually endless. You may therefore struggle to choose what to learn next. In this part, we'll wrap up the series by pointing out some possible directions for future learning. After this, the final part will be a quiz on what we covered throughout the series.Read More…
  • Code
    Android SDK
    Android SDK: Running & DebuggingAndroid preview@2x
    We have been learning how to develop Android apps in this series. In the last tutorial, we looked at connecting hardware and virtual devices to Eclipse. In this tutorial, we will explore running and debugging apps on physical devices and AVDs from Eclipse.Read More…
  • Computer Skills
    App Training
    Introducing Apple Mail for BeginnersMaillogo400
    Email is a quick and easy way of keeping in touch with friends and family the world over, not to mention the fact that it is also free to use. In this tutorial, I will show you how to send and receive email using Apple's built-in Mail application from a beginner's perspective, as well as introduce you to a number of features within the app that will help increase productivity.Read More…