29

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.

Aakash
  • 3,101
  • 8
  • 47
  • 78

11 Answers11

47

As JeffC pointed out, it is easy to essentially tell Android that you want to send something email-like and have Android give users a list of choices, which will probably include GMail. If you specifically want GMail, you have to be a bit cleverer. (Note that the correct MIME type is actually "text/plain", not "plain/text". Do to an implementation oddity, GMail seems to be the only activity which responds to the latter, but this isn't a behavior I would count on.)

The following App demonstrates the principle you can follow: actually examine all of the activities which say they can handle your SEND intent and see if any of them look like GMail.

package com.stackoverflow.beekeeper;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;

import java.util.List;

public class StackOverflowTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/plain");
        final PackageManager pm = getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
        ResolveInfo best = null;
        for (final 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);
        startActivity(intent);
    }
 }
valerybodak
  • 4,195
  • 2
  • 42
  • 53
beekeeper
  • 2,516
  • 17
  • 13
  • Thanks for the info and code beekeeper. For that matter opening any other mail program and let the user choose is totally fine. It doesnt have to be gmail. Only that program should be able to send an email which I think is accomplished by final Intent intent = new Intent(android.content.Intent.ACTION_SEND); – Aakash Oct 14 '10 at 18:38
  • How to open Yahoo mail Programatically like Gmail?? – Ketan Mehta Mar 26 '13 at 11:33
  • Maybe this answer will help you: http://stackoverflow.com/questions/9516334/how-to-open-gmail-yahoo-mail-and-rediff-mails-in-application-programmatically – valerybodak Apr 29 '13 at 09:32
  • The best answer on StackOverflow! – Faisal Shaikh Jan 25 '18 at 19:15
  • you made my day thank you! if someone want to add subject and text use this! intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ mail.getText().toString() }); intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Queries"); intent.putExtra(android.content.Intent.EXTRA_TEXT, ""); – SagitSri Nov 02 '19 at 16:37
44
try {    
    Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "your_email"));
    intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject");
    intent.putExtra(Intent.EXTRA_TEXT, "your_text");
    startActivity(intent);
} catch (ActivityNotFoundException e){
    //TODO smth
}
valerybodak
  • 4,195
  • 2
  • 42
  • 53
40

I don't know that you can specifically launch gmail. Have you tried this in your onClickListener

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
startActivity(emailIntent);  

You can find more details here: Email android intent

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
JeffCharter
  • 1,431
  • 17
  • 27
  • No I haven't tried this yet. But I definitely will. Thanks for the help Jeff. – Aakash Oct 14 '10 at 16:28
  • 5
    will crash if there is no email app installed. Please wrap in intent chooser like this: Intent.createChooser(intent, "Send email") – vidstige Nov 24 '12 at 21:41
21

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

Premkumar Manipillai
  • 2,121
  • 23
  • 24
11

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

Kartik
  • 161
  • 1
  • 5
  • While this might answer the question, you should [edit] your answer to include some additional details of how this code answers the question, to provide context to future readers. A code block by itself is not immediately useful to those who might come across the same question later on. – Hoppeduppeanut Jun 11 '19 at 07:07
3
public static void openGmail(Activity activity,String[] email, String subject, String content) {
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
    final PackageManager pm = activity.getPackageManager();
    final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
    ResolveInfo best = null;
    for(final ResolveInfo info : matches)
        if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
            best = info;
    if (best != null)
        emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);

    activity.startActivity(emailIntent);
}
sKhan
  • 9,694
  • 16
  • 55
  • 53
2
<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.

saurabh yadav
  • 567
  • 6
  • 14
1

This code will directly start the gmail application to send an email.

I found out using this post that the important part here is to find the "packageName" and the "activityInfo.name"

I wanted to only use gmail without a chooser. Note that the package name is hard coded so if Gmail changes its packagename it won't work any more.

The key to this was the setComponent where the first param is the package name and the second param is the activityInfo name.

But like i said use with caution, I repeat myself; if the user doesn't have the gmail app installed or gmail changes its package name or Activty name to send an email this (hard)code will break. Thy have been warned ;)

Here is my code

Intent myIntent = new Intent(Intent.ACTION_SEND);

PackageManager pm = getPackageManager();
Intent tempIntent = new Intent(Intent.ACTION_SEND);
tempIntent.setType("*/*");
List<ResolveInfo> resInfo = pm.queryIntentActivities(tempIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
    ResolveInfo ri = resInfo.get(i);
    if (ri.activityInfo.packageName.contains("android.gm")) {
        myIntent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
        myIntent.setAction(Intent.ACTION_SEND);
        myIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"exampleto@gmail.com"});
        myIntent.setType("message/rfc822");
        myIntent.putExtra(Intent.EXTRA_TEXT, "extra text");
        myIntent.putExtra(Intent.EXTRA_SUBJECT, "Extra subject");
        myIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("uri://your/uri/string");
    }
}
startActivity(myIntent);
Community
  • 1
  • 1
EvertvdBraak
  • 1,033
  • 1
  • 12
  • 20
1

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);
                }
Prakhar
  • 11
  • 3
  • 1
    Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? – mypetlion Sep 20 '19 at 17:21
0

if you don't get anything in this line

final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);

then replace this line with

final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 1);

gprathour
  • 14,813
  • 5
  • 66
  • 90
Kush Patel
  • 67
  • 1
  • 6
0
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);
vivek modi
  • 51
  • 7
  • Please add some additional information on why this would work – SqlKindaGuy Nov 07 '17 at 11:13
  • @ plaidDK ,In List , we are getting all packages of installed app on device.after that,check packages of gmail by put condition in for loop. if List contains gmail package, then by intent.setClassName() set gmail package,it will directly open gmail app, otherwise it opens multiple options intent.. – vivek modi Nov 08 '17 at 06:50