Skip to content

Instantly share code, notes, and snippets.

@theyann
Last active November 14, 2015 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theyann/33818d2159507c41c3bd to your computer and use it in GitHub Desktop.
Save theyann/33818d2159507c41c3bd to your computer and use it in GitHub Desktop.
Two ways to open email apps from intent, pre-filling datas. These two methods
// creating the intent with all the information we want to pre-fill
Intent intent = new Intent(Intent.ACTION_SEND);
// array of destination email addresses
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "email1@company.com", "email2@othercompany.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "my subject");
intent.putExtra(Intent.EXTRA_TEXT, "some message body");
// This type narrows down the amount of apps that will be displayed in the app choice dialog
intent.setType("message/rfc822"); // it should be mostly email apps
// Creating a string URI that will be used to populate the intent's data
String uriText = "mailto:someone@someplace.com" +
"?subject=" + Uri.encode("my subject") +
"&body=" + Uri.encode("some message body"));
// creating a URI out of it
Uri uri = Uri.parse(uriText);
// creating the actual intent
Intent intent = new Intent(Intent.ACTION_SENDTO);
// and setting the URI data
intent.setData(uri);
@theyann
Copy link
Author

theyann commented Nov 14, 2015

I have tested these two methods and their are both work at least back to API Level 16, up to level 23.
My personal favorite is the ACTION_SENDTO because the list of proposed apps in the app choice dialog is more refined (only apps that deal with mailto) and there are less possibilities to mess up.
However that being said, the ACTION_SEND method allows for multiple recipients where as the other one does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment