3

I am using the following code. Problem it that when I run this code on device. It open a Dailogbox that hase 3 options for sending a mail. "POP , email , gmail" etc. By clicking the gamil the composer appear. I just want to show Gamil mail composer directly. Instead of showing a dailog box for choosing the options. Please help me.

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

String subject = "My Subject";

emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

emailIntent.setType("text/html");

String title = "<p align='center'>" + storyTitle + "<br/>" + storyPubDate + "</p>";

String data = "<p> Sent From ABC APP Sent from my Android </p>";

            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(title + data));
startActivity(Intent.createChooser(emailIntent, "Email:"));
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105

6 Answers6

26

Try this one, Perfect

public void shareToGMail(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);
}
Kevin Driedger
  • 51,492
  • 15
  • 48
  • 55
sonida
  • 4,411
  • 1
  • 38
  • 40
7

Not sure about the need for the chooser. This is from one of my apps...

final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"jimblackler@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, content);
activity.startActivity(intent);
msal
  • 947
  • 1
  • 9
  • 30
Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • 2
    Note that setType should be text/plain not plain/text (otherwise you get an ActivityNotFoundException). Thanks for the tip! – Andrew Dec 28 '11 at 23:36
4

Try this code

 Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
 "mailto", "abc@gmail.com", null));
 emailIntent.putExtra(Intent.EXTRA_SUBJECT, "This is my subject text");
 context.startActivity(Intent.createChooser(emailIntent, null));

Ref: http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO\

Ajith M A
  • 3,838
  • 3
  • 32
  • 55
2

Below code worked for me. This will search for an email client and directly launch the new email composer with the sent values pre-populated. If no email client exists then then that should be caught to avoid crash.

The good think about this solution is that, on back press it takes you directly to your app screen where the email intent was started from.

    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
    intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
    intent.putExtra(Intent.EXTRA_TEXT, "The message");
    try {
        startActivity(intent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "Mail account not configured", Toast.LENGHT_SHORT).show();
    }

Thank you!

user_1989
  • 375
  • 4
  • 10
0

You can't bypass this popup programatically. What will happen if a user tries to access the functionality and doesn't have GMail configured?

If you want to bypass is just remove all other email clients so that GMail is the only one that can send/recieve emails. That way the popup will not appear.

Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
0

If you have several mail composer in your android device and you just want Gamil composer start for your request, you have to

emailIntent.setClassName("xxxgamil composer package name xxx", "xxxgmail composer class name xxx");  
    startActivity(emailIntent);
Joe Ho
  • 918
  • 3
  • 13
  • 28
  • I found the code. It look like your code Intent intent = new Intent(android.content.Intent.ACTION_SEND); intent.setType("text/plain"); final PackageManager pm = getPackageManager(); final List 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); – Arslan Anwar Apr 26 '11 at 11:39
  • Done. But after sending the mail. It again show to dialog box. I don't want to open it any more? Any suggestion? – Arslan Anwar Apr 27 '11 at 09:02
  • if you already specified the class name, you should got the composer activity directly, doesn't it? – Joe Ho Apr 29 '11 at 07:43