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"));

上面的代码打开一个对话框,显示以下应用程序:-蓝牙,谷歌文档,雅虎邮件,Gmail, Orkut, Skype等。

实际上,我想过滤这些列表选项。我只想显示电子邮件相关的应用程序,如Gmail和雅虎邮件。怎么做呢?

我曾在Android Market应用中看到过这样的例子。

打开Android Market应用程序 打开任何开发者指定了他/她的电子邮件地址的应用程序。(如果你找不到这样的应用程序,请打开我的应用程序:- market://details?id=com.becomputer06.vehicle.diary.free,或通过“车辆日记”搜索) 向下滚动到“开发者” 点击“发送电子邮件”

对话框只显示电子邮件应用程序,如Gmail,雅虎邮件等。它不显示蓝牙,Orkut等。什么样的代码产生这样的对话?


当前回答

这是引用自Android官方文档,我已经在Android 4.4上进行了测试,工作完美。更多例子见https://developer.android.com/guide/components/intents-common.html#Email

public void composeEmail(String[] addresses, String subject) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

其他回答

适用于所有android版本:

String[] to = {"email@server.com"};
Uri uri = Uri.parse("mailto:email@server.com")
  .buildUpon()
  .appendQueryParameter("subject", "subject")
  .appendQueryParameter("body", "body")
  .build();
Intent emailIntent = new Intent(ACTION_SENDTO, uri);
emailIntent.putExtra(EXTRA_EMAIL, TO);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

更新到Android 10,现在使用Kotlin…

fun Context.sendEmail(
  address: String?,
  subject: String?,
  body: String?,
) {
  val recipients = arrayOf(address)
  val uri = address.toUri()
    .buildUpon()
    .appendQueryParameter("subject", subject)
    .appendQueryParameter("body", body)
    .build()
  val emailIntent = Intent(ACTION_SENDTO, uri).apply {
    setData("mailto:$address".toUri());
    putExtra(EXTRA_SUBJECT, subject);
    putExtra(EXTRA_TEXT, body);
    putExtra(EXTRA_EMAIL, recipients)
  }
  val pickerTitle = getString(R.string.some_title)
  ContextCompat.startActivity(this, Intent.createChooser(emailIntent, pickerTitle, null)
}

...在更新到API 30之后,代码没有填充电子邮件客户端的主题和主体(例如Gmail)。但我在这里找到了答案:

fun Context.sendEmail(
  address: String?,
  subject: String?,
  body: String?,
) {
  val selectorIntent = Intent(ACTION_SENDTO)
    .setData("mailto:$address".toUri())
  val emailIntent = Intent(ACTION_SEND).apply {
    putExtra(EXTRA_EMAIL, arrayOf(address))
    putExtra(EXTRA_SUBJECT, subject)
    putExtra(EXTRA_TEXT, body)
    selector = selectorIntent
  }
  startActivity(Intent.createChooser(emailIntent, getString(R.string.send_email))) 

}

根据Android开发者官方文档,这是发送电子邮件意图的正确方式

添加以下代码行到你的应用程序:

Intent intent = new Intent(Intent.ACTION_SEND);//common intent 
intent.setData(Uri.parse("mailto:")); // only email apps should handle this

可选:添加主体和主题,就像这样

intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject Here");
intent.putExtra(Intent.EXTRA_TEXT, "E-mail body" );

你的问题里已经加了这句话

intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hello@example.com"});

这将是收件人的地址,这意味着用户将向您(开发人员)发送电子邮件。

主要有三种方法:

String email = /* Your email address here */
String subject = /* Your subject here */
String body = /* Your body here */
String chooserTitle = /* Your chooser title here */

1. 自定义Uri:

Uri uri = Uri.parse("mailto:" + email)
    .buildUpon()
    .appendQueryParameter("subject", subject)
    .appendQueryParameter("body", body)
    .build();

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(Intent.createChooser(emailIntent, chooserTitle));

2. 使用Intent附加:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
//emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text

startActivity(Intent.createChooser(emailIntent, "Chooser Title"));

3.支持库ShareCompat:

Activity activity = /* Your activity here */

ShareCompat.IntentBuilder.from(activity)
    .setType("message/rfc822")
    .addEmailTo(email)
    .setSubject(subject)
    .setText(body)
    //.setHtmlText(body) //If you are using HTML in your body text
    .setChooserTitle(chooserTitle)
    .startChooser();

这些解决方案对我都不起作用。这里有一个对棒棒糖有效的最小解决方案。在我的设备上,只有Gmail和本地电子邮件应用程序出现在最终的选择器列表中。

Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
                                Uri.parse("mailto:" + Uri.encode(address)));

emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Send email via..."));

下面的代码对我来说很好。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmailcom"});
Intent mailer = Intent.createChooser(intent, null);
startActivity(mailer);