Sending email using standard gmail app without cho

2019-07-21 20:25发布

问题:

I'm trying send email from my app using standard gmail app. But I get chooser all the time. How can I open standard gmail app immediately without chooser? I don't need a chooser with any application which can send email. I need only GMAIL. Thank you! Here is my code.

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

回答1:

Can you try this code.

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


回答2:

Please check this question, it will helps you mainly the third answer because of this note, It is important to note that if you are going to use this code, you check to make sure that the user has the package "com.google.android.gm" installed on their device. In any language, make sure to check for null on specific Strings and initializations.

Intent URI to launch Gmail App



回答3:

You need to use the ACTION.SENDTO intent if you want to use GMail app and not the chooser. You will also add the extras in the URI text and not on the intent.

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