1

I'm writing an android app that requires the user to confirm their subscription by clicking a link received by email. For this I would like to create a shortcut to open the preferred email-application. The following code crashes my Gmail app:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("message/rfc822");
startActivity(intent);

Would there be another solution to solve this?

keyur patel
  • 99
  • 1
  • 7
Pepster
  • 1,996
  • 1
  • 24
  • 41

2 Answers2

1

Correcting your code: message/rfc822 as the MIME type- It is not indicating -- only show email clients -- it indicates --show anything that supports message/rfc822 data. That could include some application that are not email clients.

According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:

    public void composeEmail(String[] addresses, String subject) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:"));
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
Rahul Ahuja
  • 812
  • 10
  • 24
  • This starts a message composer... I need to show the inbound email list. – Pepster May 15 '17 at 12:12
  • for me it was working a few years ago. Might be some changes in recent version. Try above code in combination with this SO answer http://stackoverflow.com/a/3982785/3142342 – Rahul Ahuja May 15 '17 at 12:23
0

Use this structure to create chooser for apps that can perform email sending.

        Intent mIntent = new Intent(Intent.ACTION_SEND);
        mIntent.setType("text/html");
        mIntent.putExtra(...); // if you need extra just add it here
        mIntent = Intent.createChooser(mIntent, "Select Email Application...");
        startActivity(mIntent);