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

Vantage FX droidTrader Now Available for UK Investors

London based Forex traders can now manage their current stock market activity while on-the-go using their favorite Android device. Vantage FX UK announces the release of a new Android application, Vantage FX

News and Rumors

LTE Android Lineup to Expand on MetroPCS

LTE handsets are multiplying on every carrier around the country and MetroPCS is proud to announce its newest additions with the Samsung Galaxy Attain 4G and the LG Connect 4G.

News and Rumors

Images and details for Verizon’s Pantech Star Q surface

Expected to cost around $99 with contract, the Star Q will be aimed at high schoolers and college-bound students.