Android:电子邮件'到'字段为空

时间:2016-06-28 11:49:27

标签: android email android-intent

我正在为酒店开发一个应用程序,在预订房间时,应用程序会向用户发送的电子邮件ID发送一封电子邮件。现在,我知道它将使用我的一个默认电子邮件客户端,即gmail。问题是,它出现在gmail的撰写消息窗口,我的消息在消息体中,但'to'field为空。有什么帮助吗?

这是代码:

public void sendmail(View vw)
    {
        name=et1.getText().toString();
        to=et2.getText().toString();
        phone=et3.getText().toString();
        addr=et4.getText().toString();
        Log.i("Send email", "");
        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_SUBJECT, "Thanks for using our app");
          emailIntent.putExtra(Intent.EXTRA_TEXT, "Hello mr"+name+"We just received an email with your details asking for a reservation:"+phone+"  "+addr+"for room number"+x+"");
          try {
              startActivity(Intent.createChooser(emailIntent, "Send mail..."));
              finish();
              Log.i("Finished sending email...", "");
           }
           catch (android.content.ActivityNotFoundException ex) {
              Toast.makeText(getBaseContext(), "There is no email client installed.", Toast.LENGTH_SHORT).show();
           }
    }


}

2 个答案:

答案 0 :(得分:1)

来自EXTRA_EMAIL的{​​{3}}:

  

String [] 包含应传递给的电子邮件地址。

这样的事情应该有效:

Intent mailIntent = new Intent(Intent.ACTION_SENDTO);
mailIntent.setData(Uri.parse("mailto:"));
mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Thanks for using our app");
mailIntent.putExtra(Intent.EXTRA_TEXT, "Hello mr"+name+"We just received an email with your details asking for a reservation:"+phone+"  "+addr+"for room number"+x+"");
if (mailIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(mailIntent);
} else {
    // no e-mail app installed
}

答案 1 :(得分:1)

试试这个:

emailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@to.com"});

电子邮件意图需要一个String数组,但是你提供了一个字符串。所以,它在你的情况下不起作用!