Opening an email client on clicking a button

AndroidEmail

Android Problem Overview


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...

Android Solutions


Solution 1 - Android

Goes like this:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
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).

Solution 2 - Android

To show only email clients use this code:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:[email protected]?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.

Solution 3 - Android

If you have a e-mail address on screen, you can just use in your xml, like this:

android:autoLink="email"

Solution 4 - Android

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);
    }

Solution 5 - Android

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"))

Solution 6 - Android

You can open email client on emulator by configuring your email address with email inbuild with email. Then when call the intent will open and send mail.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul KalidindiView Question on Stackoverflow
Solution 1 - AndroidyanchenkoView Answer on Stackoverflow
Solution 2 - AndroidmixelView Answer on Stackoverflow
Solution 3 - AndroidDaniel BeltramiView Answer on Stackoverflow
Solution 4 - AndroidRajeev JayaswalView Answer on Stackoverflow
Solution 5 - AndroidSumit JainView Answer on Stackoverflow
Solution 6 - AndroidshaileshView Answer on Stackoverflow