以编程方式发送邮件Android

时间:2014-12-13 14:53:21

标签: android

我使用它在Android中以编程方式发送电子邮件,但在Android 4.4.4中运行不好。还有另一种方法吗?这是我的代码,谢谢。

Intent i = new Intent(Intent.ACTION_SEND);  
//i.setType("text/plain"); //use this line for testing in the emulator  
i.setType("message/rfc822") ; // use from live device

i.putExtra(Intent.EXTRA_SUBJECT,"-my app-");  
i.putExtra(Intent.EXTRA_TEXT,"Hello");  
startActivity(Intent.createChooser(i, "Select your mail app"));

2 个答案:

答案 0 :(得分:1)

  

屏幕上的对话框显得很大

选择器窗口的大小取决于设备,而不是您。对于触发选择器的设备上的所有应用程序,选择器窗口的大小将相同,因此用户将期望看到非常大的"在有一个设备的设备上选择窗口。

如果您觉得选择窗口的大小应该是您想要的而不是用户期望的大小,那么您需要创建自己的选择器。您可以使用PackageManagerqueryIntentActivities()执行此操作,以查看所有响应Intent的内容并使用它来填充您自己设计的某些选择器UI。

答案 1 :(得分:0)

我希望以下代码对您有所帮助..

protected void sendEmail() {

      String[] recipients = {recipient.getText().toString()};
      Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));

      // prompts email clients only
      email.setType("message/rfc822");
      email.putExtra(Intent.EXTRA_EMAIL, recipients);
      email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
      email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
      try {
        // the user can choose the email client
         startActivity(Intent.createChooser(email, "Choose an email client from..."));
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, "No email client installed.",
                 Toast.LENGTH_LONG).show();
      }

   }

以上代码适合我! 享受!!!

相关问题