How to open Gmail Compose when a button is clicked in Android App?

Viewed 61964

I am trying to open up Gmail Compose screen when a button is clicked in my Android App. Do I need some API key for this from Google? or what do I need to do in my button onClickListener?

Any kind of insight is much appreciated.

11 Answers

You just place below code inside your click event. Will open directly gmail as compose mode, Output screenshot attached below.

Happy coding :-)

code :

Intent intent=new Intent(Intent.ACTION_SEND);
String[] recipients={"mailto@gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT,"Subject text here...");
intent.putExtra(Intent.EXTRA_TEXT,"Body of the content here...");
intent.putExtra(Intent.EXTRA_CC,"mailcc@gmail.com");
intent.setType("text/html");
intent.setPackage("com.google.android.gm");
startActivity(Intent.createChooser(intent, "Send mail"));

Output :

enter image description here

Just Place the set of code in your click event/trigger event and it will directly navigate you to the native gmail application with all the details pre-filled.

All the email attributes/details are there in the set of code below(Comments added).

Intent intent = new Intent(Intent.ACTION_SEND);
        String[] recipients = {"recipient@gmail.com"};//Add multiple recipients here
        intent.putExtra(Intent.EXTRA_EMAIL, recipients);
        intent.putExtra(Intent.EXTRA_SUBJECT, "Mail Subject"); //Add Mail Subject
        intent.putExtra(Intent.EXTRA_TEXT, "Enter your mail body here...");//Add mail body
        intent.putExtra(Intent.EXTRA_CC, "mailcc@gmail.com");//Add CC emailid's if any
        intent.putExtra(Intent.EXTRA_BCC, "mailbcc@gmail.com");//Add BCC email id if any
        intent.setType("text/html");
        intent.setPackage("com.google.android.gm");//Added Gmail Package to forcefully open Gmail App
        startActivity(Intent.createChooser(intent, "Send mail"));

#HAPPY_CODING

<TextView
 android:id="@+id/EmailId"
 android:linksClickable="true"
 android:autoLink="email"
 android:text="info@stackoverflow.com"
 />

This is the best method to send email on click of textView.

You can use Simple Intent.ACTION_SEND intent set Intent.EXTRA_EMAIL for array of emails set Intent.EXTRA_SUBJECT for subject line in email composer Explore more EXTRA options available here -> https://developer.android.com/guide/components/intents-common#Email

Here's a quick code snippet

Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("*/*");
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});
                intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
                if (intent.resolveActivity(ctx.getPackageManager()) != null) {
                    startActivity(intent);
                }
Intent intent = new Intent(Intent.ACTION_SEND).setType("text/plain")
                        .putExtra(Intent.EXTRA_EMAIL, new String[]{emails});
                List<ResolveInfo> matches = activity.getPackageManager().queryIntentActivities(intent, 0);
                ResolveInfo best = null;
                for (ResolveInfo info : matches) {
                    if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) {
                        best = info;
                    }
                }
                if (best != null) {
                    intent.setClassName(best.activityInfo.packageName,best.activityInfo.name);
                }
                activity.startActivity(intent);
Related