0

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[]{ "mail@mail.com"});
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[]{ "mail@mail.com"});
 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.

mremremre1
  • 1,036
  • 3
  • 16
  • 34

2 Answers2

2

You have 2 ways to do that:

1- ACTION_SEND

Intent intent = new Intent(Intent.ACTION_SEND);  

intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"mail@mail.com"});
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:mail@mail.com");
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.

Yann Masoch
  • 1,628
  • 1
  • 15
  • 20
1

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..."));
Lionel Port
  • 3,492
  • 23
  • 26
  • This is working but it shows a message that there are no apps that can do this action, even though the phone has an email app and the gmail app. Any ideas ? – mremremre1 Jul 31 '13 at 23:01