It’s In The Mail

One popular topic recently has been how to send emails from an Android application. You have two options: the painful and not recommended way, and the other way.

The painful and not recommended way is to send the email yourself. This would require you find a suitable SMTP client library for Java (e.g., JavaMail), get it working on Android, provide a UI for your users to provide their SMTP-sending credentials (server, SMTP authentication, etc.), then send the messages. Getting all of this to work properly is not exactly trivial. Combine that with the fact that users now have to enter email information in multiple places (your app and the built-in Email/Google Mail apps), and you can see why this approach has its issues.

There is another way. As with many Android topics, the light was initially shown by Peli and OpenIntents; I’m merely adding a few additional watts on the topic.

You can use an Intent to send an email, via one of the built-in email applications. The plus side is that the user is in full control over which account is used to send the email and can edit the email before delivery. The minus side is that you cannot use this approach to send emails behind the user’s back…which, for some, is actually a plus.

Without further ado, here’s a trivial mail-sending activity:

[sourcecode language=”java”]
public class EmailHitSample extends Activity {
public void onCreate(Bundle savedInstanceState) {

Intent msg=new Intent(Intent.ACTION_SEND);
String[] recipients={“[email protected]”};
String[] carbonCopies={“[email protected]”};

msg.putExtra(Intent.EXTRA_EMAIL, recipients);
msg.putExtra(Intent.EXTRA_CC, carbonCopies);
msg.putExtra(Intent.EXTRA_TEXT, “This is the email body”);
msg.putExtra(Intent.EXTRA_SUBJECT, “This is the email subject”);
startActivity(Intent.createChooser(msg, “This is the chooser title”));


The Intent we use has an action of ACTION_SEND. Combine that with a MIME type of “message/rfc822″, and Android knows that you are trying to send an email message.

The guts of the email — as much as you want to fill in with your application, anyway — can be handled by a series of extras. The subject is supplied by EXTRA_SUBJECT, and the message body via EXTRA_TEXT. EXTRA_EMAIL is the recipient list, as a String array. A simillar String array can be used with EXTRA_CC for those being CC’d on the message, or EXTRA_BCC for those being BCC’d on the message.

(I’d explain to our younger readers how “CC” refers to carbon copy, and how I even remember actually using carbon paper to make carbon copies of typewritten documents, but probably then I would have to beat some snarky whipper-snappers soundly with my cane, so I won’t talk about any of that.)

The preferred way to send such an Intent is to wrap it in an ACTION_CHOOSER Intent. This will, if applicable, bring up a “chooser” window for the user to pick an application to use to process the Intent, in case there are several activities set up to handle ACTION_SEND Intents for the listed MIME type. If there is only one such activity, then the chooser window will be skipped and the Intent will be routed to the one available choice. The easiest way to construct such a chooser is to use the static createChooser() method on Intent, which takes both the “real” Intent and a title to use on the chooser window, and returns an ACTION_CHOOSER Intent.

So, after contstructing all of that, we shoot off our chooser Intent, then close up this activity. Of course, most applications would be sending emails from deeper inside a real application, but in this case, we may as well just close up shop. Since startActivity() is a fire-and-forget method, we can safely finish() our activity and let our email Intent wind its way through Android. Eventually, a message composition window will appear, with our supplied data already filled in, so the user can just adjust the contents as needed and send off the message.

About author

AndroidGuys 4639 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

Huawei Announcing Phone Developed with Google This Week

According to the Wall Street Journal, Huawei is set to announce a new Android 2.2 powered phone this week. Those of you who follow Android closely know that Huawei is no stranger to the platform, having already released multiple devices. What’s so special about this phone?

News and Rumors

Orange (UK) Announces Low-Cost ‘San Francisco’ Handset

range pulled the curtain back on the first of their own branded Android handsest today. The low-cost San Francisco is a touch-only device and will be available for the attractive price of £99 ($153 US) on the pay-as-you-go Dolphin plan. The San Francisco runs Android 2.1 and features a 3.5-inch display, a 600MHz processor, 150MB internal storage, and 3-megapixel camera. Other details include your typical Android support for WiFi, Bluetooth, GPS, and FM Radio.

Deals & Steals

Amazon announces second annual 12 Days of Deals for books

You bought yourself a new Kindle Fire HD or Android tablet, eh? You do plan to use that for educational and informational reasons, right? Amazon has introduced its second annual 12