简体   繁体   English

通过Android以编程方式发送邮件

[英]Send mail programmatically Android

I am using this to send email programmatically in android but in Android 4.4.4 works bad. 我正在使用它在Android中以编程方式发送电子邮件,但在Android 4.4.4中却无法正常工作。 Is there another way to do this? 还有另一种方法吗? Here is my code, thank you. 这是我的代码,谢谢。

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

The dialog appears very big on the screen 该对话框在屏幕上显得很大

The size of the chooser window is up to the device, not you. 选择器窗口的大小取决于设备,而不是您。 The chooser window will be the same size for all apps on the device that trigger a chooser, and so the user will be expecting to see the "very big" chooser window on devices that have one. 对于触发选择器的设备上的所有应用,选择器窗口的大小均相同,因此用户将期望在具有一个选择器的设备上看到“很大”的选择器窗口。

If you feel that the size of the chooser window should be what you want rather than what your users will expect, you will need to create your own chooser. 如果您认为选择器窗口的大小应该是您想要的,而不是用户期望的大小,则需要创建自己的选择器。 You can do this using PackageManager and queryIntentActivities() to see what all responds to your Intent and using that to populate some chooser UI of your own design. 您可以使用PackageManagerqueryIntentActivities()来执行此操作,以查看所有响应您的Intent并使用它们来填充您自己设计的某些选择器UI。

I hope below code will be helpfull for you.. 希望下面的代码对您有所帮助。

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

   }

Above code works for me! 上面的代码对我有用! Enjoy!!! 请享用!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM