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等。什么样的代码产生这样的对话?


当前回答

ACTION_VIEW也有一个简单的解决方案:

  Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data = Uri.parse("mailto:customer@something.com?subject=Feedback");
        intent.setData(data);
        startActivity(intent);

其他回答

这就是我使用的方法,对我来说很有效:

//variables
String subject = "Whatever subject you want";
String body = "Whatever text you want to put in the body";
String intentType = "text/html";
String mailToParse = "mailto:";

//start Intent
Intent variableName = new Intent(Intent.ACTION_SENDTO);
variableName.setType(intentType);
variableName.setData(Uri.parse(mailToParse));
variableName.putExtra(Intent.EXTRA_SUBJECT, subject);
variableName.putExtra(Intent.EXTRA_TEXT, body);

startActivity(variableName);

这也可以让用户选择他们喜欢的电子邮件应用程序。唯一不允许你做的是设置收件人的电子邮件地址。

适用于所有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))) 

}

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

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

如果你想确保你的意图只被电子邮件应用程序处理(而不是其他短信或社交应用程序),那么使用ACTION_SENDTO动作并包括“mailto:”数据方案。例如:

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

我在https://developer.android.com/guide/components/intents-common.html#Email上找到了这个

如果你只想要电子邮件客户端,你应该使用android.content.Intent。带有数组的EXTRA_EMAIL。举个例子:

final Intent result = new Intent(android.content.Intent.ACTION_SEND);
result.setType("plain/text");
result.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { recipient });
result.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
result.putExtra(android.content.Intent.EXTRA_TEXT, body);