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:54

I notice, that this is an pretty old question but it is the first result when searching for a "Send mail" solution and all answers have a common problem:

Using Intent.ACTION_SEND and intent.setType("message/rfc822") will result in a chooser that not only shows mail apps but all apps that can handle any MIME type support by message/rfc822, e.g. .mhtml, .mht, .mime. Beside mail apps this could be Google Drive, Dropbox, Evernote, etc.

The only solution I found to limit the chooser to mail apps only is to use Intent.ACTION_SENDTO instead:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","address@example.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "My Mail");
intent.putExtra(Intent.EXTRA_TEXT   , "My Message");

try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
查看更多
放荡不羁爱自由
3楼-- · 2019-01-08 17:55

I had a similar problem with my app. I recently found this link form the official android developers site that really helps! Common Intents: Email

TL;DR:

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

Now, you will only be shown email clients!

You can add a Subject and Body by doing this:

intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body" );
查看更多
相关推荐>>
4楼-- · 2019-01-08 17:56

This will open only the Email app installed in your android phone.

        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"example@gmail.com"});
        intent.putExtra(Intent.EXTRA_SUBJECT, "email subject");
        intent.putExtra(Intent.EXTRA_TEXT, "message body");

        try {
            startActivity(Intent.createChooser(intent, "send mail"));
        } catch (ActivityNotFoundException ex) {
            Toast.makeText(this, "No mail app found!!!", Toast.LENGTH_SHORT);
        } catch (Exception ex) {
            Toast.makeText(this, "Unexpected Error!!!", Toast.LENGTH_SHORT);
        }
查看更多
迷人小祖宗
5楼-- · 2019-01-08 17:58

Using message/rfc822 type as pointed here: ACTION_SEND force sending with email solves the issue.

查看更多
不美不萌又怎样
6楼-- · 2019-01-08 18:00

Best code to restrict it to only send an email. set this type to only send an email. i.setType("message/rfc822");

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"skapis1@outlook.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(Firstclass.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
查看更多
我想做一个坏孩纸
7楼-- · 2019-01-08 18:04

This will help you.

On your button click : 

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});          
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
查看更多
登录 后发表回答