Reputation: 1036
I have a button in my app with is supposed to open the mail app of the phone and add the e-mail address that i have saved in a string. I used this :
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ "[email protected]"});
startActivity(email);
but it crashes my app. Please help.
After reading the answer of Lionel Port below i changed the code to :
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ "[email protected]"});
startActivity(Intent.createChooser(email, "Send mail..."));
which is not crashing my app but when the createChooser shows it says that the phone has no app to handle this action even though the phone has an email app and gmail.
Upvotes: 0
Views: 167
Reputation: 1630
You have 2 ways to do that:
1- ACTION_SEND
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
intent.putExtra(Intent.EXTRA_SUBJECT, "email subject"); // optional
intent.putExtra(Intent.EXTRA_TEXT, "email body"); // optional
intent.setType("message/rfc822"); // useful define which kind of app to perform the action
startActivity(Intent.createChooser(intent, "Send Email"));
2- ACTION_SENDTO
Uri uri = Uri.parse("mailto:[email protected]");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra(Intent.EXTRA_SUBJECT, "email subject"); // optional
intent.putExtra(Intent.EXTRA_TEXT, "email body"); // optional
startActivity(intent);
The first solution will offer you the choice to send the content with all apps accepting the type format "message/rfc822".
The second one will offer you the choice to send the content only with email apps present on the device (native email, Gmail or other if installed).
I prefer the second solution.
Upvotes: 2
Reputation: 3542
You need to check the logcat entry to see what is crashing your app. It possible that a different app to what you expect is being opened to handle the ACTION_SEND event. To make sure the correct app is being open, let the user decide by forcing the display of a chooser.
startActivity(Intent.createChooser(email, "Send mail..."));
Upvotes: 1