使用Gmail发送邮件时选择发送,但发现邮件正文为空。

6

我正在使用以下代码:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",email, null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, text);
activity.startActivity(Intent.createChooser(emailIntent, "Send feedback to xyz"));

我已经用这个功能有两年时间了,一直都工作得很好。用户可以选择信息客户端,并使用预先填充的数据发送反馈。对于所有邮件客户端都能很好地工作。

最近我注意到,如果我选择 Gmail 客户端,消息正文仍然为空,但在其他邮件客户端中,正文会被填充文本。

有什么想法吗?


1
也许这是同样的问题,并且看起来已经得到解决了。https://dev59.com/z1IH5IYBdhLWcg3wgvAU - kochizufan
1
我讨厌这种问题,一开始它能正常工作,之后却不行了,而你并没有改变任何东西。很高兴看到你的回答,谢谢! - Berkay Turancı
6个回答

13

感谢帮助

进行了许多建议答案的测试。 添加"text/plain"或"message/rfc822"会使我的应用停止提供邮件客户端。

找到了解决我的问题的答案: https://dev59.com/z1IH5IYBdhLWcg3wgvAU#59365539

对我来说最有趣的部分是有两个意图:

Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, text);
emailIntent.setSelector( selectorIntent );
activity.startActivity(Intent.createChooser(emailIntent, "Send feedback to XYZ"));

这解决了问题。


邮件不能以这种方式发送!它们似乎消失了,既不显示在发件箱中,也没有发送。 - Hissaan Ali
我不相信添加"type"为"text/plain"可以阻止邮件客户端的提供,这应该是其他原因。 - user924
非常感谢您的回答,您解决了我的难题!当我添加text/plain或message/rfc822时,我也遇到了同样的问题,但这个双重意图的解决方法正常工作。 - Fernando Jascovich

1

我在Gmail中使用“body”属性,在其他电子邮件中使用“EXTRA_TEXT”。 我已针对不同的电子邮件应用程序进行了测试,例如三星电子邮件、一加电子邮件和LG电子邮件,它们似乎支持“EXTRA_TEXT”,但是Gmail支持“body”属性。

 fun composeEmailMessage(context: Context,  subject: String, body: String, emails: Array<String> = arrayOf()) {
    val intent = Intent(Intent.ACTION_SENDTO)
    intent.data = Uri.parse("mailto:")
    intent.putExtra(Intent.EXTRA_EMAIL, emails)
    intent.putExtra(Intent.EXTRA_SUBJECT, subject)
    intent.putExtra(Intent.EXTRA_TEXT, body)//other emails app
    intent.putExtra("body", body)//gmail
    if (intent.resolveActivity(context.packageManager) != null) {
        context.startActivity(Intent.createChooser(intent, "Send email via..."))
    }
}

1

最近我遇到了同样的问题。在搜索时,我发现以下是最好的解决方案(kotlin)(至少对我而言):

fun sendEmail(email: String, subject: String, message: String) {
    val emailIntent = Intent(Intent.ACTION_SEND)
    emailIntent.data = Uri.parse("mailto:")
    emailIntent.type = "text/plain"
    emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
    emailIntent.putExtra(Intent.EXTRA_TEXT, message)

    val sendIntent = Intent.createChooser(emailIntent, "Please Choose Email Client...")
    sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

    try {
        context.startActivity(sendIntent)
    } catch (e: Exception) {
        Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
    }
}

1
emailIntent.type = "text/plain" 这一行会出现错误 "没有应用程序可以执行此操作"。但是当它被删除时,一切都正常。 - Yonko Kilasi

0
我正在使用以下代码,并且对于每个电子邮件客户端都有效。 示例:
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email id of receiver"});
intent.putExtra(Intent.EXTRA_SUBJECT, "This is the subject of the email client");
intent.putExtra(Intent.EXTRA_TEXT, "This is the body of the email client");

// this line is for attaching file
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Send Email"));

0

我曾经遇到过类似的问题。这个方法对我有用:不要使用

"mailto"

而是要使用

"mailto:"

我还将"Uri.fromParts"替换为"Uri.parse"


0

如果要发送带有正文的电子邮件,请使用message/rfc822。

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("message/rfc822");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "to1@example.com", "to2@example.com" });
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject of the email");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Content of the email");
startActivity(sendIntent);

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接