11

I just want to know how to open Mail Composer in Android.

With iOS, I would do something like this :

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[controller setSubject:@"Mail subject"];
[controller setMessageBody:@"Mail body" isHTML:bool];
[controller setToRecipients:recipientsList];
if(controller) [self presentModalViewController:controller animated:YES];

How about Android ?

Thanks a lot.

Rob
  • 14,827
  • 20
  • 65
  • 104
  • refer this previous post http://stackoverflow.com/questions/10614908/how-to-send-an-email-in-android-2-2/10615156#10615156 – Aerrow Jul 11 '12 at 08:42

4 Answers4

27
Intent intent=new Intent(Intent.ACTION_SEND);
String[] recipients={"xyz@gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT,"abc");
intent.putExtra(Intent.EXTRA_TEXT,"def");
intent.putExtra(Intent.EXTRA_CC,"ghi");
intent.setType("text/html");
startActivity(Intent.createChooser(intent, "Send mail"));
AkashG
  • 7,627
  • 3
  • 26
  • 43
  • I'll do it as soon as I can. I just have a little problem : if I do `intent.putExtra(Intent.EXTRA_TEXT, "Hello
    Just a little mail."); intent.setType("text/html");`, in the mail composer the text isn't html formatted. I tried `Intent.EXTRA_HTML_TEXT` but it makes my app stop running.
    – Rob Jul 11 '12 at 08:51
  • 3
    EDIT : finally I got it working with `intent.setType("text/html"); intent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("Hello
    Just a little test."));`. Thank you again.
    – Rob Jul 11 '12 at 08:54
3

The list of apps can be limited to email apps only by using ACTION_SENDTO.

public void composeEmail(String[] addresses, String subject) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

See https://developer.android.com/guide/components/intents-common.html#Email

Danyal Aytekin
  • 3,857
  • 2
  • 32
  • 42
2

If you want to open only the email clients, then:

Intent intent = new Intent(Intent.ACTION_SEND);
String[] recipients = {"wantedEmail@gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, "emailTitle:");
intent.putExtra(Intent.EXTRA_CC, "ghi");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Send mail"));

Mostly similar to the accepted answer, with different MIME type.

Menelaos Kotsollaris
  • 4,517
  • 6
  • 50
  • 61
1

Like this:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, new String[]{emailCC});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailText);
    context.startActivity(Intent.createChooser(emailIntent, context.getString("send email using:")));

You can find more details here: http://mobile.tutsplus.com/tutorials/android/android-email-intent/

Nermeen
  • 15,570
  • 5
  • 52
  • 68