Last active
November 14, 2015 17:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.