Android 如何将字符串变量附加到电子邮件

Android 如何将字符串变量附加到电子邮件,android,string,email,variables,android-studio,Android,String,Email,Variables,Android Studio,在Android Studio中,我希望通过点击按钮发送电子邮件。在我开始改变之前,我一直在使用下面的代码,直到我弄清楚到底发生了什么 String[] TO = {"ABC@yahoo.com.au"}; String[] CC = {"xyz@gmail.com"}; Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setData(Uri.parse("mailto:"));

在Android Studio中,我希望通过点击按钮发送电子邮件。在我开始改变之前,我一直在使用下面的代码,直到我弄清楚到底发生了什么

    String[] TO = {"ABC@yahoo.com.au"};
    String[] CC = {"xyz@gmail.com"};
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_CC, CC);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Some message added in here");

    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MainActivity.this,
                "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }
这工作得很好,在我的手机上显示的电子邮件内容与预期一样,但是电子邮件内容“此处添加了一些消息”行显然是硬编码的。显然,我希望通过执行以下操作来添加我自己的内容

    String content = "Information I want to send";
    emailIntent.putExtra(Intent.EXTRA_TEXT, content);
但由于某些原因,电子邮件内容是空白的。为什么可以识别字符串“内容”,但不能识别字符串变量x?

检查此示例 通过查看您的代码,我只发现设置中存在问题

  • emailIntent.setType(文本/普通)

  • 可能是您正在使用Gmail发送邮件(所以您必须查看第二个示例)

  • 发送电子邮件(到电话电子邮件客户端)

    发送电子邮件(到Gmail)

    Gmail不检查额外的Intent字段,因此为了使用此Intent,您需要使用Intent.ACTION\u SENDTO并传递一个主题和主体URL编码的mailto:URI

    String uriText =
        "mailto:youremail@gmail.com" + 
        "?subject=" + Uri.encode("some subject text here") + 
        "&body=" + Uri.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")); 
    

    你能填上完整的活动代码吗?这并不能真正回答老年退休金计划的问题。我很想知道他为什么也有这个问题,以及如何解决它…@iaindownie我已经提到他做错了什么。第二个例子效果很好,非常感谢。尽管你提到我做错了什么,但这并不能解释为什么我做错了。String x=“hello world”和“hello world”都是字符串。为什么一个人工作而不是另一个人other@gavin这是基于应用程序之间的应用程序与其他应用程序的通信方式,大多数应用程序更喜欢通用方式,这是第一个示例,有些应用程序有自己的规则。
    String uriText =
        "mailto:youremail@gmail.com" + 
        "?subject=" + Uri.encode("some subject text here") + 
        "&body=" + Uri.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"));