Email body empty when select to send email by Gmail

大兔子大兔子 提交于 2020-04-14 08:15:07

问题


I'm using this code:

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

for 2 years. And until now everything worked fine. User can select message client and send feedback with prefilled data inside. It worked fine for all mail clients. Recently noticed that if I select gmail client - body of message remains empty, but on other mail clients body is filled with text.

Any ideas?


回答1:


Thanks for help

Made tests with lots of suggested answers. adding "text/plain" or "message/rfc822" made my app to stop offering mail clients.

Fount this answer that fixed my issue: https://stackoverflow.com/a/59365539/973233

Most interesting part for me is having 2 intents:

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

This solved problem.




回答2:


Recently i encountered the same issue. While searching, i found this as being the best solution (kotlin) (at least for myself):

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



回答3:


To send an email with a body, use 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);

Hope this helps.




回答4:


I'm using the following code and is working for every email client. Example:

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


来源:https://stackoverflow.com/questions/59836984/email-body-empty-when-select-to-send-email-by-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!