-1

So I'm trying to launch a prepopulated email client with data. The content gets populated fine, however my problem is that when launching the intent, I wanted it to only show email clients to select from. Instead, it shows Gmail, Adding to EverNote, Android Beam, Bluetooth, and some others.

I don't know if its an issue with lollipop that broke this functionality or not, as one of my managers sent me code that worked fine for him a few years ago.

My code is:

private void openEmailClient(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");

    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{getResources().getString(R.string.contact_feedback_email_address)});
    intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.contact_feedback_email_subject_android));

    try{
        startActivity(Intent.createChooser(intent,intentEmailString));
    } catch(android.content.ActivityNotFoundException ex){
        Log.e(EMAIL_FAIL_TAG, EMAIL_FAIL);
        ex.printStackTrace();
    }
}
Mike
  • 2,132
  • 3
  • 20
  • 33
Ayohaych
  • 5,099
  • 7
  • 29
  • 51

3 Answers3

1

when you will change your intent.setType like below you will get

intent.setType("text/plain");

Use

android.content.Intent.ACTION_SENDTO

(new Intent(Intent.ACTION_SENDTO);) to get only the list of e-mail clients, with no facebook or other apps. Just the email clients.

I wouldn't suggest you get directly to the email app. Let the user choose his favorite email app. Don't constrain him.

If you use ACTION_SENDTO, putExtra does not work to add subject and text to the intent. Use Uri to add the subject and body text.

We can use message/rfc822 instead of "text/plain" as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.

message/rfc822 supports MIME Types of .mhtml, .mht, .mime

EDIT

 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:someone@example.com"));
 intent.putExtra("subject", "my subject");
 intent.putExtra("body", "my message");
 startActivity(intent);

its working ...

Kartheek
  • 7,104
  • 3
  • 30
  • 44
  • 1
    How can I set the subject and body using a Uri? Using ACTION_SENDTO shows things like bluetooth etc. I want to allow the users to choose their favourite mail app but the problem is, applications like Bluetooth etc appear too. – Ayohaych May 21 '15 at 13:50
  • Yep. Was going to mark my solution as the answer but I marked yours instead thanks – Ayohaych May 22 '15 at 11:04
0

Try like this it working fine for me...

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:abc@gmail.com"));    
intent.putExtra(Intent.EXTRA_SUBJECT, "Test App");  
intent.putExtra(Intent.EXTRA_TEXT, "Email Body");
startActivity(intent);

Note: it only work if you have email address.

For more information please refer this link Android - Is there a foolproof way to only show possible EMAIL clients?

Community
  • 1
  • 1
Anjali Tripathi
  • 1,477
  • 9
  • 28
0

So I solved it. Not ideally but it works better than anything else I have tried.

I followed the google docs on doing it, which says to do this:

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
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

and it now works. This just finds a default app for handling mail. I'm not sure how it decides, but in my case it opened GMail. On a device without GMail installed, such as the Galaxy S5, it opened their mail client and prompted the user to set up email. Doesn't give choice of app but it works

Ayohaych
  • 5,099
  • 7
  • 29
  • 51