我想从我的应用程序发送一封电子邮件。所以我使用了以下代码。
String uriText = "[email protected]" + "?subject=" + URLEncoder.encode("Subject") + "&body=" + URLEncoder.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send Email"));
我已经配置了 Gmail 和电子邮件应用程序。我在 Nexus S (JellyBean) 和 HTC T-Mobile G2 (GingerBread) 上进行了测试。它们都显示“没有应用程序可以执行此操作。”。
有人知道这里出了什么问题吗?
如果您使用
Intent.setData
发送电子邮件,请将代码更改为:
String uriText = "mailto:[email protected]" +
"?subject=" + URLEncoder.encode("Subject") +
"&body=" + URLEncoder.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send Email"));
Uri 应该是“mailto”
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
intent.putExtra(Intent.EXTRA_SUBJECT,"Order summary of Coffee");
intent.putExtra(Intent.EXTRA_TEXT,BodyOfEmail);
if(intent.resolveActivity(getPackageManager())!=null) {
startActivity(intent);
}
我一整天都在尝试打开
com.android.mms/.ui.ComposeMessageActivity
(华为)。try {
startActivity(
Intent(Intent.ACTION_SENDTO, Uri.parse("mmsto:"))
)
} catch (e: Exception) {
Log.e(LOG_TAG, "Exception: ${e.message}")
}
或者如果您想知道,
intent
是否可以解决:
if (intent.resolveActivity(this.packageManager) == null) {
Log.w(LOG_TAG, "Could not resolve intent: ${intent.action}")
}
以下代码对我有用,并且更加可靠和灵活。而且,它是用 Kotlin 编写的:)
fun sendEmail(context: Context, mailTo: String? = null, subject: String? = null, bodyText: String? = null) {
val emailIntent = Intent(Intent.ACTION_SENDTO)
val uri = Uri.parse("mailto:${mailTo ?: ""}").buildUpon() // "mailto:" (even if empty) is required to open email composers
subject?.let { uri.appendQueryParameter("subject", it) }
bodyText?.let { uri.appendQueryParameter("body", it) }
emailIntent.data = uri.build()
try {
context.startActivity(emailIntent)
} catch (e: ActivityNotFoundException) {
// Handle error properly
}
}