Using Android Intent.ACTION_SEND for sending email

前端 未结 17 1197
野趣味
野趣味 2020-11-29 23:07

I\'m using Intent.ACTION_SEND to send an email. However, when I call the intent it is showing choices to send a message, send an email, and also to

相关标签:
17条回答
  • 2020-11-29 23:51

    This saved my day. It sends composed text message directly to gmail app:

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                            "mailto","togmail.com", null));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report message");
    emailIntent.putExtra(Intent.EXTRA_TEXT, edt_msg.getText().toString());
    startActivity(Intent.createChooser(emailIntent, "Send email..."));
    
    0 讨论(0)
  • 2020-11-29 23:52

    I'm not taking credit for this answer but I believe it gives the best answer for this post.

    It's a common misconception to use text/plain or text/html. This will trigger any application that can handle plain or HTML text files without any context, including Google Drive, Dropbox, Evernote and Skype.

    Instead use a ACTION_SENDTO, providing the mailto: Uri

    intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
    

    You can then proceed using the chooser as suggested through the other answers.

    Answered by @PaulLammertsma here Android email chooser

    0 讨论(0)
  • 2020-11-29 23:52

    This is a combination of Jack Dsilva and Jignesh Mayani solutions:

        try
        {
            Intent gmailIntent = new Intent(Intent.ACTION_SEND);
            gmailIntent.setType("text/html");
    
            final PackageManager pm = _activity.getPackageManager();
            final List<ResolveInfo> matches = pm.queryIntentActivities(gmailIntent, 0);
            String gmailActivityClass = null;
    
            for (final ResolveInfo info : matches)
            {
                if (info.activityInfo.packageName.equals("com.google.android.gm"))
                {
                    gmailActivityClass = info.activityInfo.name;
    
                    if (gmailActivityClass != null && !gmailActivityClass.isEmpty())
                    {
                        break;
                    }
                }
            }
    
            gmailIntent.setClassName("com.google.android.gm", gmailActivityClass);
            gmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "yourmail@gmail.com" });
            gmailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            gmailIntent.putExtra(Intent.EXTRA_CC, "cc@gmail.com"); // if necessary
            gmailIntent.putExtra(Intent.EXTRA_TEXT, "Email message");
            gmailIntent.setData(Uri.parse("yourmail@gmail.com"));
            this._activity.startActivity(gmailIntent);
        }
    
        catch (Exception e)
        {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.putExtra(Intent.EXTRA_EMAIL, new String[] { "yourmail@gmail.com" });
            i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            i.putExtra(Intent.EXTRA_CC, "cc@gmail.com"); // if necessary
            i.putExtra(Intent.EXTRA_TEXT, "Email message");
            i.setType("plain/text");
            this._activity.startActivity(i);
        }
    

    So, at first it will try to open gmail app and in case a user doesn't have it then the second approach will be implemented.

    0 讨论(0)
  • 2020-11-29 23:55

    Thanks to the Open source developer, cketti for sharing this concise and neat solution. It's the only method that worked for me.

    String mailto = "mailto:bob@example.org" +
        "?cc=" + "alice@example.com" +
        "&subject=" + Uri.encode(subject) +
        "&body=" + Uri.encode(bodyText);
    
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse(mailto));
    
    try {
      startActivity(emailIntent);
    } catch (ActivityNotFoundException e) {
      //TODO: Handle case where no email app is available
    }
    

    And this is the link to his/her gist.

    0 讨论(0)
  • 2020-11-29 23:56

    First solution: try to be more specific in your Intent parameters. Add a message recipient for instance

    emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"user@example.com"});
    

    Second solution: use the package manager to find all applications capable of sending a message and select the only those you want to use.

    0 讨论(0)
提交回复
热议问题