如何使用android studio编写代码来发送电子邮件

Sil*_*sha 12 android

我打算使用android studio开发一个Android移动应用程序,用户可以在其中提供电子邮件地址和密码.然后该密码应发送到提到的电子邮件地址.任何机构都可以共享任何代码片段来执行此操作吗?

Psy*_*her 23

如果您想在后台发送电子邮件,请参阅此处

如果用户在屏幕上等待使用以下方法:

protected void sendEmail() {
      Log.i("Send email", "");

      String[] TO = {"someone@gmail.com"};
      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, "Your subject");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");

      try {
         startActivity(Intent.createChooser(emailIntent, "Send mail..."));
         finish();
         Log.i("Finished sending email...", "");
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, 
         "There is no email client installed.", Toast.LENGTH_SHORT).show();
      }
   }
Run Code Online (Sandbox Code Playgroud)


小智 5

If you use Intent.ACTION_SEND android show all communicatons app. If you want show only email client you can use the following code.

 Intent mailIntent = new Intent(Intent.ACTION_VIEW);
 Uri data = Uri.parse("mailto:?subject=" + "subject text"+ "&body=" + "body text " + "&to=" + "destination@mail.com");
 mailIntent.setData(data);
 startActivity(Intent.createChooser(mailIntent, "Send mail..."));
Run Code Online (Sandbox Code Playgroud)