在没有选择器的情况下使用标准 gmail 应用程序发送电子邮件

Sending email using standard gmail app without chooser

我正在尝试使用标准 Gmail 应用程序从我的应用程序发送电子邮件。 但我总是有选择的余地。 如何在没有选择器的情况下立即打开标准 gmail 应用程序? 我不需要任何可以发送电子邮件的应用程序的选择器。 我只需要 GMAIL。 谢谢! 这是我的代码。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
intent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"mymail@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT   , "Text");
try {
    startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

如果您想使用 GMail 应用程序,您需要使用 ACTION.SENDTO 意图 而不是选择者。您还将在 URI 文本中添加额外内容 而不是故意的。

Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("example@gmail.com") +
        "?subject=" + Uri.encode("the subject") +
        "&body=" + Uri.encode("the body of the message");
Uri uri = Uri.parse(uriText);

send.setData(uri);

startActivity(Intent.createChooser(send, "Send Email..."))

请检查这个问题,因为这个注释,它主要对你的第三个答案有帮助,重要的是要注意,如果你要使用这个代码,你检查以确保用户有包 "com.google.android.gm" 安装在他们的设备上。在任何语言中,确保检查特定字符串和初始化是否为 null。

Intent URI to launch Gmail App

你能试试这个代码吗?

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent.setType("plain/text");
    emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"someone@gmail.com"});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Yo");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hi");
    startActivity(emailIntent);