Android : how to open a mail composer view?
Asked Answered
F

4

12

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.

Finedraw answered 11/7, 2012 at 8:37 Comment(1)
refer this previous post #10615408Postmeridian
K
29
Intent intent=new Intent(Intent.ACTION_SEND);
String[] recipients={"[email protected]"};
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"));
Karrikarrie answered 11/7, 2012 at 8:40 Comment(2)
I'll do it as soon as I can. I just have a little problem : if I do intent.putExtra(Intent.EXTRA_TEXT, "<html>Hello<br/>Just a little mail.</html>"); 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.Finedraw
EDIT : finally I got it working with intent.setType("text/html"); intent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<html>Hello<br/><b>Just a little test.</b></html>"));. Thank you again.Finedraw
M
4

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

Merriweather answered 23/7, 2014 at 10:40 Comment(0)
H
2

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

Intent intent = new Intent(Intent.ACTION_SEND);
String[] recipients = {"[email protected]"};
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.

Hemielytron answered 22/4, 2015 at 3:2 Comment(0)
G
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/

Gamboa answered 11/7, 2012 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.