Using Android Intent.ACTION_SEND for sending email

2019-01-08 17:37发布

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 send via bluetooth. I want it to only show choices to send an email. How can I do this?

13条回答
何必那么认真
2楼-- · 2019-01-08 17:40

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

查看更多
孤傲高冷的网名
3楼-- · 2019-01-08 17:41
Intent email = new Intent(android.content.Intent.ACTION_SEND);  
                    email.setType("application/octet-stream");    

EDIT:
You could try setting type to "message/rfc822" as well.

try this....

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-08 17:42

[Solution for API LEVEL >=15]

I've finally succeded in sending email WITH attachments to ONLY email clients. I write it here because it took me a lot of time and it may be usefull to others.

The problem is:

  • Intent.ACTION_SENDTO takes Data URI (so you can specify "mailto:" schema) BUT it does not accept Intent:EXTRA_STREAM.

  • Intent.ACTION_SEND accepts Intent:EXTRA_STREAM (so you can add attachment) BUT it takes only Type (not Data URI so you cannot specify "mailto:" schema).

So Intent.ACTION_SEND lets the user choose from several Activities, even if you setType("message/rfc822"), because that App/Activities can manage all file types (tipically GDrive/Dropbox Apps) and so even email message files.

The solution is in the setSelector method. With this method you can use Intent.ACTION_SENDTO to select the Activity, but then send the Intent.ACTION_SEND Intent.

Here my solution code (the attachment came from a FileProvider, but it could be any file):

{
    Intent emailSelectorIntent = new Intent(Intent.ACTION_SENDTO);
    emailSelectorIntent.setData(Uri.parse("mailto:"));

    final Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"address@mail.com"});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    emailIntent.setSelector( emailSelectorIntent );

    Uri attachment = FileProvider.getUriForFile(this, "my_fileprovider", myFile);
    emailIntent.putExtra(Intent.EXTRA_STREAM, attachment);

    if( emailIntent.resolveActivity(getPackageManager()) != null )
        startActivity(emailIntent);
}
查看更多
一夜七次
5楼-- · 2019-01-08 17:42

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..."));
查看更多
该账号已被封号
6楼-- · 2019-01-08 17:48

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.

查看更多
疯言疯语
7楼-- · 2019-01-08 17:50

@Ganapathy:try this code for display gmail

Intent gmail = new Intent(Intent.ACTION_VIEW);
                gmail.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
                gmail.putExtra(Intent.EXTRA_EMAIL, new String[] { "jckdsilva@gmail.com" });
                gmail.setData(Uri.parse("jckdsilva@gmail.com"));
                gmail.putExtra(Intent.EXTRA_SUBJECT, "enter something");
                gmail.setType("plain/text");
                gmail.putExtra(Intent.EXTRA_TEXT, "hi android jack!");
                startActivity(gmail);
查看更多
登录 后发表回答