Start Email-Activity with Preset Data (via Intents)

3 votes · 7 comments

This code starts the email activity with pre-set data. On the emulator it might get a "No application can perform this action" failure, although it works on a real phone. Code picked up on anddev

raw ·
copy
· download
/* Create the Intent */ final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); /* Fill it with Data */ emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"to@email.com"}); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject"); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text"); /* Send it off to the Activity-Chooser */ context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Add a comment

7 Comments

Hi, I need send email like HTML. So I try emailIntent.setType("text/html"); but email isn't formated like HTML page. I see HTML tags. Do you have any idea how can I do it?

Reply · May 14, 2009, 8:37 p.m.

Exactly what I was looking for. Works well! Thanks, feri

Reply · May 21, 2009, 9:45 p.m.

a typo: Intent.createChooser(intent... should be Intent.createChooser(emailIntent...

Reply · May 21, 2009, 10:46 p.m.

Thanks for the tip -- the typo is corrected! :)

Reply · May 22, 2009, 12:17 a.m.

Intent intent = new Intent(Intent.ACTION_SEND); String[] tos = { "shenrenkui@gmail.com" }; String[] ccs = { "shenrenkui@gmail.com" }; intent.putExtra(Intent.EXTRA_EMAIL, tos); intent.putExtra(Intent.EXTRA_CC, ccs); intent.putExtra(Intent.EXTRA_TEXT, "body"); intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); intent.setType("message/rfc882"); Intent.createChooser(intent, "Choose Email Client");

Reply · Sept. 26, 2009, 11:03 p.m.

This is perfect. I was looking for code to do just this.

Reply · March 18, 2011, 3:55 p.m.

Just as a note this won't work on the emulator. You'll need to push it out to a device for emailing to work.

Reply · March 21, 2011, 3:58 a.m.