I am designing an app in which i need to open an email client on clicking a button. The email client should be opened with a pre-defined subject and 'to' address. Is there a way to attain this? Please provide me the solution and a code example if possible...
Asked
Active
Viewed 5.2k times
64
-
Possible duplicate of [How to open Email program via Intents (but only an Email program)](http://stackoverflow.com/questions/3312438/how-to-open-email-program-via-intents-but-only-an-email-program) – mixel May 16 '16 at 10:19
-
Please have a look at [this](https://stackoverflow.com/a/54602382/413306) answer to get ONLY email clients – Ayyappa Feb 09 '19 at 01:41
6 Answers
93
Goes like this:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
Alternatively, you could use IntentFactory.getSendEmailIntent(String mailTo, String mailCC, String subject, CharSequence body, File attachment).

yanchenko
- 56,576
- 33
- 147
- 165
-
1When i click the button its displaying that NO APPLCATIONS CAN PERFORM THIS ACTION.... Whats the solution??? – Rahul Kalidindi Apr 29 '10 at 07:01
-
5This does not work reliably today - it will allow user to choose Facebook Messenger for example, instead of GMail. Please see: https://stackoverflow.com/a/15022153/1735603 (accepted answer in thread is wrong as well, beware) – Firzen Jun 03 '19 at 14:59
-
This solution did not work for me in 2020 DEC. My working solution from official doc: https://stackoverflow.com/a/62877003/2155858 – Rajeev Jayaswal Dec 19 '20 at 13:54
49
To show only email clients use this code:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:recipient@example.com?subject=" + Uri.encode(subject) + "&body=" + Uri.encode(body));
intent.setData(data);
startActivity(intent);
If you already chose default email client then it will launch it. Otherwise it will show a list of available email clients.

mixel
- 25,177
- 13
- 126
- 165
-
this works perfectly...i am astonished why it worked without permissions ? – anshulkatta Aug 20 '17 at 07:33
-
@anshulkatta Because it does not perform any action requiring permissions. It just opens activity that can send email. – mixel Aug 20 '17 at 14:45
-
How do you encode the subject and body in case they contain characters that would break things? – Tyler Jun 05 '20 at 21:08
-
@mixel After some testing, you need to do `URLEncoder.encode().replace("+", "%20")` otherwise the email will have pluses instead of spaces. – Tyler Jun 09 '20 at 18:57
-
7
If you have a e-mail address on screen, you can just use in your xml, like this:
android:autoLink="email"

Daniel Beltrami
- 756
- 9
- 22
5
Ok - now the above answer no longer works for me in year 2020. I found something mentioned on google official developer sites which worked for me.
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);
}

Rajeev Jayaswal
- 1,423
- 1
- 20
- 22
-
Not working for me on Android 10. `EXTRA_SUBJECT` and `EXTRA_TEXT` are being ignored – Mark Aug 26 '21 at 10:36
-
For Android 10 and higher we need to add query in manifest to make `resolveActivity` work as said in answer https://stackoverflow.com/a/65166064/10413998 – remain4life Aug 18 '22 at 17:15
1
Prefer to use constants if available like for intent.type ClipDescription.MIMETYPE_TEXT_PLAIN
Kotlin:
val intent = Intent(Intent.ACTION_SEND)
intent.type = ClipDescription.MIMETYPE_TEXT_PLAIN
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("emailId 1", "emailId 2"))
intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject for email")
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Description for email")
startActivity(Intent.createChooser(intent,"Send Email"))

Sumit Jain
- 1,100
- 1
- 14
- 27