Android - Creating Intents: Email
Hello all,
I really enjoyed the Android Basics by Google and Udacity lesson Practice with Intents so I am taking some time to fully explore each of the intents in the Android Common Intents documentation. The one I have most recently looked at is Email.
First I just created a simple button to activate the intent which looks like this:
![Android App showing a single EMAIL button.](https://static.wixstatic.com/media/b86045_bd5d24021e49473dace965df8ce76b81~mv2.png/v1/fill/w_304,h_500,al_c,q_85,enc_auto/b86045_bd5d24021e49473dace965df8ce76b81~mv2.png)
And the code in activity_main.xml looks like this:
<Button
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:layout_gravity="center"
android:layout_margin="@dimen/margin"
android:onClick="sendEmail"
android:text="Email"/>
Then I created an activity in MainActivity.java to call the intent when the button is clicked. To launch an email app all you need to do is include this code.
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Notice that the line:
intent.setData(Uri.parse("mailto:"));
makes it so that the intent will only be completed by email apps, ignoring text and other messenger apps. There are also other lines of code you can add to pre-set certain aspects of the email. I experimented with a number of these and ended up with some code that looks like this, with explanatory comments:
public void sendEmail(View view) {
// How to set up a new email intent
Intent intent = new Intent(Intent.ACTION_SENDTO);
//This line ensures only email apps are used as opposed to text or
// messenger apps being included.
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
// Optional line of code to add text to the subject line of email
intent.putExtra(Intent.EXTRA_SUBJECT, "Got Mail");
// Optional line of code to add text to the body of the email
intent.putExtra(Intent.EXTRA_TEXT, "This is the message.");
// This set up a string array containing an email address is sent to
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"example@gmail.com"});
// This string array contains two email addresses to add to the CC part of email
intent.putExtra(Intent.EXTRA_CC, new String[]{
"examplecc@gmail.com",
"examplecc2@gmail.com"});
// This string array has put each email on a different line
// but functions the same to add to the BCC part of email
intent.putExtra(Intent.EXTRA_BCC, new String[]{
"examplebcc@gmail.com",
"examplebcc2@gmail.com"});
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
All of this should allow you to use the Email intent in your app and modify it in a number of ways.
I welcome any comments or questions about this, especially corrections of anything I can improve. For example, I was unsure of how to use Intent.EXTRA_STREAM to add attachments.
Links: