我想从我的应用发送一封电子邮件。所以我使用了以下代码。
String uriText = "abcd@gmail.com" + "?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)。它们都显示“没有应用可以执行此操作。”。
有谁知道这里有什么问题?
答案 0 :(得分:9)
如果您要使用ACTION_SENDTO
,Uri
应使用mailto:
或smsto:
计划。所以,试试mailto:abcd@gmail.com
。
答案 1 :(得分:8)
如果您使用Intent.setData
发送电子邮件,请将代码更改为:
String uriText = "mailto:someone@example.com" +
"?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"));
答案 2 :(得分:0)
以下代码为我工作,并且更加可靠和灵活。另外,它是用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
}
}
答案 3 :(得分:0)
Uri应该是“ mailto”
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT,"Order summary of Coffee");
intent.putExtra(Intent.EXTRA_TEXT,BodyOfEmail);
if(intent.resolveActivity(getPackageManager())!=null) {
startActivity(intent);
}