0

I found an algorithm to send an email through an Android app. When I run it, it opens a list of apps I can choose to send it, however, I only wanted to use the Gmail app.

This is the code:

protected void sendEmail(String subject, String text) {
    Log.i("Send email", "");
    String[] TO = {"email@gmail.com"};

    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("message/rfc822");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, text);

    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();
        Log.i("Finished sending email!", "");
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getApplicationContext(), "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }
}

Thanks in advance!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Check my question [sending-email-with-attachment-from-asset-folder](https://stackoverflow.com/questions/30252769/sending-email-with-attachment-from-asset-folder) – Ramesh sambu Dec 18 '17 at 13:44
  • Possible duplicate of [Intent URI to launch Gmail App](https://stackoverflow.com/questions/3470042/intent-uri-to-launch-gmail-app) – Manohar Dec 18 '17 at 13:50
  • 1
    "I only wanted to use the Gmail app" -- what do your *users* want to use? What if they do not want to use Gmail? – CommonsWare Dec 18 '17 at 13:53
  • Probably he is making an app for his own use! @CommonsWare – Xenolion Dec 18 '17 at 13:54
  • @CommonsWare it's for a problem report feature. It has a predetermined email account to which it sends the email. Right now, it allows Gmail, the standard E-mail app, Whatsapp and Google Drive, which doesn't make sense in this context. – Miguel Andrade Dec 18 '17 at 14:13
  • Start by using `ACTION_SENDTO` instead of `ACTION_SEND`, which will better constrain the options. Beyond that, you did not answer my question: what about your users who do not want to use Gmail, or are not allowed to use Gmail? – CommonsWare Dec 18 '17 at 14:15
  • @CommonsWare I understand. At most, it should allow any email app, but it doesn't make sense to send an email through Google Drive. I tried it, but it says "No apps can perform this action." – Miguel Andrade Dec 18 '17 at 14:22
  • You have no means of filtering to only email apps. Using `ACTION_SENDTO` with a `mailto:` `Uri` will make it fairly likely that only email apps will show up. – CommonsWare Dec 18 '17 at 14:26
  • @CommonsWare thanks, I tried that and it worked. – Miguel Andrade Dec 18 '17 at 14:45

2 Answers2

0

Implicit Intents declare a general action to perform, in your case every app that has an intent filter of an e-mail client, this can be Gmail, Outlook or whatever e-mail client that your device has.

In this case you can try this code:

        Intent sendIntentGmail = new Intent(Intent.ACTION_VIEW);
        sendIntentGmail.setType("plain/text");
        sendIntentGmail.setData(Uri.parse(TextUtils.join(",", addresses)));
        sendIntentGmail.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
        sendIntentGmail.putExtra(Intent.EXTRA_EMAIL, addresses);
        if (subject != null) sendIntentGmail.putExtra(Intent.EXTRA_SUBJECT, subject);
        if (body != null) sendIntentGmail.putExtra(Intent.EXTRA_TEXT, body);
        mContext.startActivity(sendIntentGmail);
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
  • This is undocumented, unsupported, and may break in the future. for more information see [this](https://stackoverflow.com/questions/3470042/intent-uri-to-launch-gmail-app) – salmanseifian Dec 18 '17 at 14:28
-1

You can use below code:

 Intent mailClient = new Intent(Intent.ACTION_VIEW);
 mailClient.setClassName("com.google.android.gm", 
 "com.google.android.gm.ConversationListActivity");
 startActivity(mailClient);
salmanseifian
  • 1,082
  • 3
  • 9
  • 26