Sending email using standard gmail app without cho

2019-07-21 20:29发布

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

3条回答
神经病院院长
2楼-- · 2019-07-21 20:59

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楼-- · 2019-07-21 21:18

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..."))
查看更多
时光不老,我们不散
4楼-- · 2019-07-21 21:22

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);
查看更多
登录 后发表回答