Android Intent(或代码)在(outlook,gmail)中打开电子邮件编辑器

时间:2018-08-07 15:37:49

标签: android android-intent

我想知道如何打开用于以gmail或Outlook编写电子邮件的活动。到现在为止,我只知道ho将被重定向到例如gmail,但是我还将打开“ composer”(或其他内容)并添加一个电子邮件地址。我已经尝试过:

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

但不起作用

测试应用程序的完整源代码

 public class MainActivity extends AppCompatActivity {

private static final String GOOGLE_MAIL = "com.google.android.gm";
private static final String OUTLOOK = "com.microsoft.office.outlook";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    test(GOOGLE_MAIL);

}


private void test(String mail) {

    Intent intent = getPackageManager().getLaunchIntentForPackage(mail);
    intent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
    startActivity(intent);
}

}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

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