April 23, 2014

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 {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(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”);
msg.setType(“message/rfc822″);
startActivity(Intent.createChooser(msg, “This is the chooser title”));

finish();
}
}
[/sourcecode]

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.

Article Tags

Related Posts