Send Email Intent

2018-12-31 14:12发布

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, "emailaddress@emailaddress.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");

startActivity(Intent.createChooser(intent, "Send Email"));

The above code opens a dialog showing following apps:- Bluetooth, Google Docs, Yahoo Mail, Gmail, Orkut, Skype etc.

Actually, I want to filter these list-options. I want to show only email related apps e.g. Gmail, Yahoo Mail. How to do it?

I've seen such example on 'Android Market' application.

  1. Open Android Market app
  2. Open any application where developer has specified his/her email address. (If you can't find such app just open my app:- market://details?id=com.becomputer06.vehicle.diary.free , OR search by 'Vehicle Diary')
  3. Scroll down to 'DEVELOPER'
  4. Click on 'Send Email'

The dialog shows only email Apps e.g. Gmail, Yahoo Mail etc. It does not show Bluetooth, Orkut etc. What code produces such dialog?

28条回答
明月照影归
2楼-- · 2018-12-31 14:40

The accepted answer doesn't work on the 4.1.2. This should work on all platforms:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto","abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

Hope this helps.

Update: According to marcwjj, it seems that on 4.3, we need to pass string array instead of a string for email address to make it work. We might need to add one more line:

intent.putExtra(Intent.EXTRA_EMAIL, addresses); // String[] addresses

Ref link

查看更多
栀子花@的思念
3楼-- · 2018-12-31 14:40

Use this:

boolean success = EmailIntentBuilder.from(activity)
        .to("support@example.org")
        .cc("developer@example.org")
        .subject("Error report")
        .body(buildErrorReport())
        .start();

use build gradle :

compile 'de.cketti.mailto:email-intent-builder:1.0.0'
查看更多
与风俱净
4楼-- · 2018-12-31 14:42

Try:

intent.setType("message/rfc822");
查看更多
永恒的永恒
5楼-- · 2018-12-31 14:42

Maybe you should try this: intent.setType("plain/text");

I found it here. I've used it in my app and it shows only E-Mail and Gmail options.

查看更多
临风纵饮
6楼-- · 2018-12-31 14:43

If you want to target Gmail then you could do the following. Note that the intent is "ACTION_SENDTO" and not "ACTION_SEND" and the extra intent fields are not necessary for Gmail.

String uriText =
    "mailto:youremail@gmail.com" + 
    "?subject=" + Uri.encode("your subject line here") + 
    "&body=" + Uri.encode("message body here");

Uri uri = Uri.parse(uriText);

Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
if (sendIntent.resolveActivity(getPackageManager()) != null) {
   startActivity(Intent.createChooser(sendIntent, "Send message")); 
}
查看更多
骚的不知所云
7楼-- · 2018-12-31 14:46

This works for me perfectly fine:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("mailto:" + address));
    startActivity(Intent.createChooser(intent, "E-mail"));
查看更多
登录 后发表回答