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


当前回答

用Anko - kotlin

context.email(email, subject, body)

其他回答

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

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

我几乎在android 11上使用了所有的答案,但它们不能正常工作。其中一些没有将mailto放在所需的字段上,另一些甚至根本不起作用。所以我确实阅读了新的文档,发现mailto电子邮件应该在一个数组中,所以对我有用的最后是在这里。 不管怎样,谢谢你的回答,毕竟他们帮了大忙。

//mail me
findViewById<TextView>(R.id.mailme).setOnTouchListener { _, _ ->
    try {
        val mail: Array<String> = arrayOf("somemail@cc.com")
        val mailme = Intent(Intent.ACTION_SENDTO).apply {
            data = Uri.parse("mailto:")
            putExtra(Intent.EXTRA_EMAIL, mail)
            putExtra(Intent.EXTRA_TEXT, "Hey We Need Your Help With This Issue.")
            putExtra(Intent.EXTRA_SUBJECT, "At Logs Calculator, We Need Your Help !")
        }
        startActivity(mailme)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    true
}

科特林:

val email: String = getEmail()
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:$email" )
startActivity(intent)

我正在用Kotlin更新Adil的答案,

val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:") // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, Array(1) { "test@email.com" })
intent.putExtra(Intent.EXTRA_SUBJECT, "subject")
if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent)
} else {
    showSnackBar(getString(R.string.no_apps_found_to_send_mail), this)
}

如果您想要以Gmail为目标,那么您可以执行以下操作。注意,意图是“ACTION_SENDTO”而不是“ACTION_SEND”,额外的意图字段对于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")); 
}