Source code for an example Android 'send email' function/method

Android FAQ: Can you share some source code for an Android send email method?

If you need a simple Android 'send email' function/method, this source code should do the trick for you:

public void sendEmailMessage(String subject, String body, String chooserTitle) {
    Intent mailIntent = new Intent();
    mailIntent.setAction(Intent.ACTION_SEND);
    mailIntent.setType("message/rfc822");
    mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {""});
    mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    mailIntent.putExtra(Intent.EXTRA_TEXT, body);
    startActivity(Intent.createChooser(mailIntent, chooserTitle));
}

It doesn't handle email attachments -- I'll demonstrate that in a different example -- but if you call it from within your Android app, it will do the following:

  • Start the email "chooser", so a user with multiple email clients (Gmail, Yahoo, etc.) will be able to choose which client they want to use to send the message.
  • Let you set the title of the chooser.
  • Populate the email client Subject and Body fields with the text you provide.

This method doesn't need the EXTRA_EMAIL setting I've shown (which is the "To:" field of your email message), but I wanted to leave that in there so you can see how to set additional email fields. Depending on your needs, you can set it as well as the CC and BCC fields like this:

mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"joe@example.com"});
mailIntent.putExtra(Intent.EXTRA_CC, new String[] {"fred@flinstone.com"});
mailIntent.putExtra(Intent.EXTRA_BCC, new String[] {"barney@rubble.com"});

You can also add a file attachment to your email in Android like this:

mailIntent.putExtra(Intent.EXTRA_STREAM, FILE_URI);

I'll show a more complete 'email with attachment' example in a future Android tutorial, but I wanted to share this to help you get started.

More complete version

I just had to make a few changes to my application, and ending up touching this 'send email' method again. As a result, here's the latest version of the source code:

/**
 * Starts an Android email intent.
 * The activity, subject, and bodyText fields are required.
 * You can pass a null field for the chooserTitle, to, cc, and bcc fields if 
 * you don't want to specify them.
 */
public static void emailResultsToUser(Activity activity,
                                      String subject,
                                      String bodyText,
                                      String chooserTitle,
                                      String[] toRecipients,
                                      String[] ccRecipients,
                                      String[] bccRecipients) {
  Intent mailIntent = new Intent();
  mailIntent.setAction(Intent.ACTION_SEND);
  mailIntent.setType("message/rfc822");
  mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
  mailIntent.putExtra(Intent.EXTRA_TEXT, bodyText);

  if (toRecipients != null) {
      mailIntent.putExtra(Intent.EXTRA_EMAIL, toRecipients);
  }
  if (ccRecipients != null) {
      mailIntent.putExtra(Intent.EXTRA_CC, ccRecipients);
  }
  if (bccRecipients != null) {
      mailIntent.putExtra(Intent.EXTRA_BCC, bccRecipients);
  }
  if (chooserTitle==null) chooserTitle = "Send Email";
  activity.startActivity(Intent.createChooser(mailIntent, chooserTitle));
}

In summary, I hope this Android 'send email' example source code is helpful.