Android ACTION_SEND意图不填充主题或正文

时间:2022-11-15 12:13:13

I have code in my app which lets the user send an email to the developer. It is supposed to prepopulate the To field, the Subject field, and the body field. HOwever, when I run, it populates To but ignores the other EXTRAs like Subject, Body, and Chooser text. I'm seeing this behavior on two test devices: one running Lollipop (Verizon Samsung Galaxy Note 4) and one running Jelly Bean 4.2.2 (Samsung Fascinate on CM10.1, although I don't know if this has bearing on the issue.

我的应用程序中有代码,用户可以向开发人员发送电子邮件。它应该预先填充To字段,Subject字段和body字段。 HOwever,当我运行时,它会填充To但忽略其他EXTRA,如Subject,Body和Chooser文本。我在两个测试设备上看到了这种行为:一个运行Lollipop(Verizon三星Galaxy Note 4)和一个运行Jelly Bean 4.2.2(三星Fascinate在CM10.1上,虽然我不知道这是否与此问题有关。

private void sendHelpEmail() {
    Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
    // prompts email clients only
    email.setType("message/rfc822");

    email.putExtra(Intent.EXTRA_EMAIL, new String[] {getString(R.string.about_email)});
    email.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.login_help_subject));
    email.putExtra(Intent.EXTRA_TEXT, getString(R.string.login_help_body, classButton.text(), Txt_Student.getText().toString()));

    try {
        // the user can choose the email client
        startActivity(Intent.createChooser(email, getString(R.string.login_help_chooser)));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(mThis, R.string.login_help_error, Toast.LENGTH_LONG).show();
        }
}

Why would Subject and Body get ignored when the To email is populated?

为什么在填充To电子邮件时会忽略Subject和Body?

3 个解决方案

#1


The following code works for me (just tried it):

以下代码适用于我(只是尝试过):

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"foo@bar.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
  startActivity(Intent.createChooser(i, "Choose email...");
} catch (android.content.ActivityNotFoundException ex) {
    // handle edge case where no email client is installed
}

#2


Try this method it works for me.

尝试这种方法对我有用。

private void sendMail() {

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "xx@xx.com", null));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT,getResources().getString(R.string.mail_txt));
    startActivity(Intent.createChooser(emailIntent, "Send email..."));

}         

#3


Literally just had this problem and here's how I solved it:

字面上只是有这个问题,这就是我如何解决它:

Check if the email client (in my particular case, Gmail) isn't just re-using an unsent rough instead of composing a new email as when doing that the client seems to ignore both Intent.EXTRA_SUBJECT and Intent.EXTRA_TEXT.

检查电子邮件客户端(在我的特定情况下,Gmail)是否只是重新使用未发送的粗糙而不是编写新电子邮件,因为客户端似乎忽略了Intent.EXTRA_SUBJECT和Intent.EXTRA_TEXT。

#1


The following code works for me (just tried it):

以下代码适用于我(只是尝试过):

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"foo@bar.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
  startActivity(Intent.createChooser(i, "Choose email...");
} catch (android.content.ActivityNotFoundException ex) {
    // handle edge case where no email client is installed
}

#2


Try this method it works for me.

尝试这种方法对我有用。

private void sendMail() {

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "xx@xx.com", null));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT,getResources().getString(R.string.mail_txt));
    startActivity(Intent.createChooser(emailIntent, "Send email..."));

}         

#3


Literally just had this problem and here's how I solved it:

字面上只是有这个问题,这就是我如何解决它:

Check if the email client (in my particular case, Gmail) isn't just re-using an unsent rough instead of composing a new email as when doing that the client seems to ignore both Intent.EXTRA_SUBJECT and Intent.EXTRA_TEXT.

检查电子邮件客户端(在我的特定情况下,Gmail)是否只是重新使用未发送的粗糙而不是编写新电子邮件,因为客户端似乎忽略了Intent.EXTRA_SUBJECT和Intent.EXTRA_TEXT。