How to send Email with attachment using only email apps? - android

There are two requirements:
Email with attachment
In the Intent chooser, there should be only Email apps.
What I have known/done:
Intent.ACTION_SENDTO with intent.setData(Uri.parse("mailto:")) can make sure that there are only Email apps in Intent chooser but it will not bring attachment(For some apps like Gmail it will, but there are also many apps that will ignore attachment).
Intent.ACTION_SEND can send Email with attachment. However, in Intent chooser, there will be apps that are actually not Email apps but can response to Intent.ACTION_SEND. Using intent.setType("message/rfc822") can reduce number of those apps but not all.
References this answer: https://stackoverflow.com/a/8550043/3952691 and nearly succeed in my goals. My code is as below:
private static void sendFeedbackWithAttachment(Context context, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
if (resolveInfos.isEmpty()) {
Toast.makeText(context, context.getString(R.string.error_activity_not_found),
Toast.LENGTH_SHORT).show();
} else {
// ACTION_SEND may be replied by some apps that are not email apps. However,
// ACTION_SENDTO doesn't allow us to choose attachment. As a result, we use
// an ACTION_SENDTO intent with email data to filter email apps and then send
// email with attachment by ACTION_SEND.
List<LabeledIntent> intents = new ArrayList<>();
Uri uri = getLatestLogUri();
for (ResolveInfo info : resolveInfos) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage(info.activityInfo.packageName);
i.setClassName(info.activityInfo.packageName, info.activityInfo.name);
i.putExtra(Intent.EXTRA_EMAIL, new String[] { Def.Meta.FEEDBACK_EMAIL });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_STREAM, uri);
intents.add(new LabeledIntent(i, info.activityInfo.packageName,
info.loadLabel(context.getPackageManager()), info.icon));
}
Intent chooser = Intent.createChooser(intents.remove(0),
context.getString(R.string.send_feedback_to_developer));
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new LabeledIntent[intents.size()]));
context.startActivity(chooser);
}
}
However, on some devices(For example, Xiaomi 2S with MIUI V5, I don't know if this can be influenced by a third-party rom), the result is an empty Intent chooser. And it seems that above Android 6.0, Intent.EXTRA_INITIAL_INTENTS has some bugs(Custom intent-chooser - why on Android 6 does it show empty cells?, and another one: https://code.google.com/p/android/issues/detail?id=202693).
As a result, I don't know how to achieve my goals. Please help me, thank you in advance.

Try the below code to Send a mail
String filename="filename.vcf";
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));

There are two ways to do this
OPTION 1
Intent emailIntent = new Intent(
android.content.Intent.ACTION_VIEW);
//Option 1
Uri data = Uri.parse("mailto:?subject=" + "blah blah subject"
+ "&body=" + "blah blah body" + "&to=" + "sendme#me.com");
emailIntent.setData(data);
startActivity(Intent.createChooser(emailIntent, ""));
Result
OPTION 2
It works perfactly except it wont filter out FTP
//Option 2
emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
final String[] toRecipients = new String[] { "sendme#me.com", "", };
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, toRecipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "blah blah subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
Html.fromHtml("blah blah body"));
startActivity(Intent.createChooser(emailIntent, ""));
Result
Both ways have minor flaws I show you both ways it is now upto you to pick one.

Related

How to share text from the "EditText" or "Textbox" via a share button

I have a textbox(EditText) in my app & a button, what i want to do is that when anyone tap the button, the text written in the textbox(EditText) get copied & this text can be shared to any of the app such as - Messaging, Gmail, Ymail, etc.
Now What i am doing is getting the text from the "EditText" & storing it into a new variable (string) say 'a' & now applying the Intent "ACTION_SEND_MULTIPLE"
Here is the CODE for Intent
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"example#gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "a");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, a);
startActivity(Intent.createChooser(emailIntent, "Share it via..."));
I'm not sure what your problem is, but this is how you get text from an editText
String mString= et.getText().toString();
Then put it in the share intent
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, mString);
shareIntent.setType("text/plain");
startActivity(shareIntent);
If you want to send it as an email only, and only allow email clients to respond to the intent, it goes something like this.
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("mail to address")
+ "?subject=" + Uri.encode("subject here")
+ "&body=" + Uri.encode("body here");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send..."));
This allows you to enter the subject field & mailto field...etc
You need "createChooser" maybe:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, edittext.getText().toString());
startActivity(Intent.createChooser(intent, "chooser title"));

How to send mail only through email clients in my android application

How to send mail through only email clients from my android application.
I am using the below code in my application but its opeing messeges and bluetooth also. I need only Email clients like Gmail or yahoo.
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "mailto#gmail.com");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject");
startActivity(Intent.createChooser(emailIntent, "Email:"))
Just Go on to Use this Code...It will always invoke your default Email Client.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + subject + "&body=" + body);
intent.setData(data);
startActivity(intent);
Is this of any help? This one is using the gmail client it seems.
Intent URI to launch Gmail App
I use this way to avoid choosing other app but default email client.
Intent it = new Intent(Intent.ACTION_SEND);
it.setType("text/plain");
it.putExtra(Intent.EXTRA_EMAIL, new String[]{emailAddr});
it.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
it.putExtra(Intent.EXTRA_TEXT, emailContent);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + emailAddr));
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityInfo info = emailIntent.resolveActivityInfo(mContext.getPackageManager(), PackageManager.MATCH_DEFAULT_ONLY);
if (info != null) {
it.setPackage(info.packageName);
}
mContext.startActivity(it);
I hope it can help you ~
I made it work using this:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("mp3/3gp");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "recording voice");
Uri eri=Uri.parse(rlm.getPlayList().get(position).get("songPath"));
emailIntent.putExtra(Intent.EXTRA_STREAM,eri);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "hello, it is the recorded file of our conversassion");
emailIntent.putExtra(Intent.EXTRA_STREAM,rlm.getPlayList().get(position).get("songPath") );
RecordedList.this.startActivity(Intent.createChooser(emailIntent, "Send Email"));

Send Email Intent

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, "emailaddress#emailaddress.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
startActivity(Intent.createChooser(intent, "Send Email"));
The above code opens a dialog showing the following apps:- Bluetooth, Google Docs, Yahoo Mail, Gmail, Orkut, Skype, etc.
Actually, I want to filter these list options. I want to show only email-related apps e.g. Gmail, and Yahoo Mail. How to do it?
I've seen such an example on the 'Android Market application.
Open the Android Market app
Open any application where the developer has specified his/her email address. (If you can't find such an app just open my app:- market://details?id=com.becomputer06.vehicle.diary.free, OR search by 'Vehicle Diary')
Scroll down to 'DEVELOPER'
Click on 'Send Email'
The dialog shows only email Apps e.g. Gmail, Yahoo Mail, etc. It does not show Bluetooth, Orkut, etc. What code produces such dialog?
UPDATE
Official approach:
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);
}
}
Ref link
OLD ANSWER
The accepted answer doesn't work on the 4.1.2. This should work on all platforms:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Update: According to marcwjj, it seems that on 4.3, we need to pass string array instead of a string for email address to make it work. We might need to add one more line:
intent.putExtra(Intent.EXTRA_EMAIL, addresses); // String[] addresses
There are three main approaches:
String email = /* Your email address here */
String subject = /* Your subject here */
String body = /* Your body here */
String chooserTitle = /* Your chooser title here */
1. Custom Uri:
Uri uri = Uri.parse("mailto:" + email)
.buildUpon()
.appendQueryParameter("subject", subject)
.appendQueryParameter("body", body)
.build();
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(Intent.createChooser(emailIntent, chooserTitle));
2. Using Intent extras:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
//emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text
startActivity(Intent.createChooser(emailIntent, "Chooser Title"));
3. Support Library ShareCompat:
Activity activity = /* Your activity here */
ShareCompat.IntentBuilder.from(activity)
.setType("message/rfc822")
.addEmailTo(email)
.setSubject(subject)
.setText(body)
//.setHtmlText(body) //If you are using HTML in your body text
.setChooserTitle(chooserTitle)
.startChooser();
when you will change your intent.setType like below you will get
intent.setType("text/plain");
Use android.content.Intent.ACTION_SENDTO to get only the list of e-mail clients, with no facebook or other apps. Just the email clients.
Ex:
new Intent(Intent.ACTION_SENDTO);
I wouldn't suggest you get directly to the email app. Let the user choose his favorite email app. Don't constrain him.
If you use ACTION_SENDTO, putExtra does not work to add subject and text to the intent. Use Uri to add the subject and body text.
EDIT:
We can use message/rfc822 instead of "text/plain" as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.
message/rfc822 supports MIME Types of .mhtml, .mht, .mime
This is quoted from Android official doc, I've tested it on Android 4.4, and works perfectly. See more examples at https://developer.android.com/guide/components/intents-common.html#Email
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);
}
}
A late answer, although I figured out a solution which could help others:
Java version
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:abc#xyz.com"));
startActivity(Intent.createChooser(emailIntent, "Send feedback"));
Kotlin version
val emailIntent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:abc#xyz.com")
}
startActivity(Intent.createChooser(emailIntent, "Send feedback"))
This was my output (only Gmail + Inbox suggested):
I got this solution from the Android Developers site.
This works for me:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "me#somewhere.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
startActivity(Intent.createChooser(intent, "Email via..."));
Most importantly: Use the ACTION_SENDTO action rather than the ACTION_SEND action. I've tried it on a couple of Android 4.4 devices and:
It correctly limits the chooser pop-up to only display email applications (Email, Gmail, Yahoo Mail etc); and
It correctly inserts the email address and subject into the email.
Try:
intent.setType("message/rfc822");
This is the proper way to send the e-mail intent according to the Android Developer Official Documentation
Add these lines of code to your app:
Intent intent = new Intent(Intent.ACTION_SEND);//common intent
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
Optional: Add the body and subject, like this
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject Here");
intent.putExtra(Intent.EXTRA_TEXT, "E-mail body" );
You already added this line in your question
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hello#example.com"});
This will be the recipient's address, meaning the user will send you (the developer) an e-mail.
Finally come up with best way to do
String to = "test#gmail.com";
String subject= "Hi I am subject";
String body="Hi I am test body";
String mailTo = "mailto:" + to +
"?&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(body);
Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse(mailTo));
startActivity(emailIntent);
Works on all android Versions:
String[] to = {"email#server.com"};
Uri uri = Uri.parse("mailto:email#server.com")
.buildUpon()
.appendQueryParameter("subject", "subject")
.appendQueryParameter("body", "body")
.build();
Intent emailIntent = new Intent(ACTION_SENDTO, uri);
emailIntent.putExtra(EXTRA_EMAIL, TO);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Updated for Android 10, now using Kotlin...
fun Context.sendEmail(
address: String?,
subject: String?,
body: String?,
) {
val recipients = arrayOf(address)
val uri = address.toUri()
.buildUpon()
.appendQueryParameter("subject", subject)
.appendQueryParameter("body", body)
.build()
val emailIntent = Intent(ACTION_SENDTO, uri).apply {
setData("mailto:$address".toUri());
putExtra(EXTRA_SUBJECT, subject);
putExtra(EXTRA_TEXT, body);
putExtra(EXTRA_EMAIL, recipients)
}
val pickerTitle = getString(R.string.some_title)
ContextCompat.startActivity(this, Intent.createChooser(emailIntent, pickerTitle, null)
}
...after updating to API 30, the code did not fill the subject and body of the email client (e.g Gmail). But I found an answer here:
fun Context.sendEmail(
address: String?,
subject: String?,
body: String?,
) {
val selectorIntent = Intent(ACTION_SENDTO)
.setData("mailto:$address".toUri())
val emailIntent = Intent(ACTION_SEND).apply {
putExtra(EXTRA_EMAIL, arrayOf(address))
putExtra(EXTRA_SUBJECT, subject)
putExtra(EXTRA_TEXT, body)
selector = selectorIntent
}
startActivity(Intent.createChooser(emailIntent, getString(R.string.send_email)))
}
If you want only the email clients you should use android.content.Intent.EXTRA_EMAIL with an array. Here goes an example:
final Intent result = new Intent(android.content.Intent.ACTION_SEND);
result.setType("plain/text");
result.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { recipient });
result.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
result.putExtra(android.content.Intent.EXTRA_TEXT, body);
The following code works for me fine.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc#gmailcom"});
Intent mailer = Intent.createChooser(intent, null);
startActivity(mailer);
Edit: Not working anymore with new versions of Gmail
This was the only way I found at the time to get it to work with any characters.
doreamon's answer is the correct way to go now, as it works with all characters in new versions of Gmail.
Old answer:
Here is mine. It seems to works on all Android versions, with subject and message body support, and full utf-8 characters support:
public static void email(Context context, String to, String subject, String body) {
StringBuilder builder = new StringBuilder("mailto:" + Uri.encode(to));
if (subject != null) {
builder.append("?subject=" + Uri.encode(Uri.encode(subject)));
if (body != null) {
builder.append("&body=" + Uri.encode(Uri.encode(body)));
}
}
String uri = builder.toString();
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
context.startActivity(intent);
}
None of these solutions were working for me. Here's a minimal solution that works on Lollipop. On my device, only Gmail and the native email apps appear in the resulting chooser list.
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("mailto:" + Uri.encode(address)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Send email via..."));
From Android developers docs:
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);
}
}
Most of these answers work only for a simple case when you are not sending attachment. In my case I need sometimes to send attachment (ACTION_SEND) or two attachments (ACTION_SEND_MULTIPLE).
So I took best approaches from this thread and combined them. It's using support library's ShareCompat.IntentBuilder but I show only apps which match the ACTION_SENDTO with "mailto:" uri. This way I get only list of email apps with attachment support:
fun Activity.sendEmail(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null) {
val originalIntent = createEmailShareIntent(recipients, subject, file, text, secondFile)
val emailFilterIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
val originalIntentResults = packageManager.queryIntentActivities(originalIntent, 0)
val emailFilterIntentResults = packageManager.queryIntentActivities(emailFilterIntent, 0)
val targetedIntents = originalIntentResults
.filter { originalResult -> emailFilterIntentResults.any { originalResult.activityInfo.packageName == it.activityInfo.packageName } }
.map {
createEmailShareIntent(recipients, subject, file, text, secondFile).apply { `package` = it.activityInfo.packageName }
}
.toMutableList()
val finalIntent = Intent.createChooser(targetedIntents.removeAt(0), R.string.choose_email_app.toText())
finalIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray())
startActivity(finalIntent)
}
private fun Activity.createEmailShareIntent(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null): Intent {
val builder = ShareCompat.IntentBuilder.from(this)
.setType("message/rfc822")
.setEmailTo(recipients.toTypedArray())
.setStream(file)
.setSubject(subject)
if (secondFile != null) {
builder.addStream(secondFile)
}
if (text != null) {
builder.setText(text)
}
return builder.intent
}
in Kotlin if anyone is looking
val emailArrray:Array<String> = arrayOf("travelagentsupport#kkk.com")
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:") // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, emailArrray)
intent.putExtra(Intent.EXTRA_SUBJECT, "Inquire about travel agent")
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Following Code worked for me!!
import android.support.v4.app.ShareCompat;
.
.
.
.
final Intent intent = ShareCompat.IntentBuilder
.from(activity)
.setType("application/txt")
.setSubject(subject)
.setText("Hii")
.setChooserTitle("Select One")
.createChooserIntent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
This works for me perfectly fine:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:" + address));
startActivity(Intent.createChooser(intent, "E-mail"));
If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:
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);
}
}
I found this in https://developer.android.com/guide/components/intents-common.html#Email
Using intent.setType("message/rfc822"); does work but it shows extra apps that not necessarily handling emails (e.g. GDrive). Using Intent.ACTION_SENDTO with setType("text/plain") is the best but you have to add setData(Uri.parse("mailto:")) to get the best results (only email apps). The full code is as follows:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.setData(Uri.parse("mailto:IT#RMAsoft.NET"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Email from My app");
intent.putExtra(Intent.EXTRA_TEXT, "Place your email message here ...");
startActivity(Intent.createChooser(intent, "Send Email"));
If you want to target Gmail then you could do the following. Note that the intent is "ACTION_SENDTO" and not "ACTION_SEND" and the extra intent fields are not necessary for Gmail.
String uriText =
"mailto:youremail#gmail.com" +
"?subject=" + Uri.encode("your subject line here") +
"&body=" + Uri.encode("message body here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(sendIntent, "Send message"));
}
I am updating Adil's answer in Kotlin,
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:") // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, Array(1) { "test#email.com" })
intent.putExtra(Intent.EXTRA_SUBJECT, "subject")
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
} else {
showSnackBar(getString(R.string.no_apps_found_to_send_mail), this)
}
Kotlin:
val email: String = getEmail()
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:$email" )
startActivity(intent)
String sendEmailTo = "abc#xyz.com";
String subject = "Subject";
String body = "Body";
Uri uri = Uri.parse("mailto:"+sendEmailTo+"?subject="+subject+"&body="+body);
startActivity(new Intent(Intent.ACTION_VIEW, uri);
This worked for me. This will only show the mailing application in the intent chooser.
Additionally:
One problem that i faced with this method is I was unable to add space in the suggestions and body text.
So, to put spaces in the suggestion or body text then replace the space with %20
Please use the below code :
try {
String uriText =
"mailto:emailid" +
"?subject=" + Uri.encode("Feedback for app") +
"&body=" + Uri.encode(deviceInfo);
Uri uri = Uri.parse(uriText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(uri);
startActivity(Intent.createChooser(emailIntent, "Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ContactUsActivity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
}
Maybe you should try this: intent.setType("plain/text");
I found it here. I've used it in my app and it shows only E-Mail and Gmail options.
Compose an email in the phone email client:
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, ""));
Use this:
boolean success = EmailIntentBuilder.from(activity)
.to("support#example.org")
.cc("developer#example.org")
.subject("Error report")
.body(buildErrorReport())
.start();
use build gradle :
compile 'de.cketti.mailto:email-intent-builder:1.0.0'
This is what I use, and it works for me:
//variables
String subject = "Whatever subject you want";
String body = "Whatever text you want to put in the body";
String intentType = "text/html";
String mailToParse = "mailto:";
//start Intent
Intent variableName = new Intent(Intent.ACTION_SENDTO);
variableName.setType(intentType);
variableName.setData(Uri.parse(mailToParse));
variableName.putExtra(Intent.EXTRA_SUBJECT, subject);
variableName.putExtra(Intent.EXTRA_TEXT, body);
startActivity(variableName);
This will also let the user choose their preferred email app. The only thing this does not allow you to do is to set the recipient's email address.

Send email via gmail

I have a code the fires intent for sending email
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, msg);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Start.this,
"There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
But when this intent is fired I see many item in the list like sms app , gmail app, facebook app and so on.
How can I filter this and enable only gmail app (or maybe just email apps)?
Use android.content.Intent.ACTION_SENDTO (new Intent(Intent.ACTION_SENDTO);) to get only the list of e-mail clients, with no facebook or other apps. Just the email clients.
I wouldn't suggest you get directly to the email app. Let the user choose his favorite email app. Don't constrain him.
If you use ACTION_SENDTO, putExtra does not work to add subject and text to the intent. Use Uri to add the subject and body text.
Example
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("email#gmail.com") +
"?subject=" + Uri.encode("the subject") +
"&body=" + Uri.encode("the body of the message");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
The accepted answer doesn't work on the 4.1.2. This should work on all platforms:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Hope this helps.
Igor Popov's answer is 100% correct, but in case you want a fallback option, I use this method:
public static Intent createEmailIntent(final String toEmail,
final String subject,
final String message)
{
Intent sendTo = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode(toEmail) +
"?subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(message);
Uri uri = Uri.parse(uriText);
sendTo.setData(uri);
List<ResolveInfo> resolveInfos =
getPackageManager().queryIntentActivities(sendTo, 0);
// Emulators may not like this check...
if (!resolveInfos.isEmpty())
{
return sendTo;
}
// Nothing resolves send to, so fallback to send...
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("text/plain");
send.putExtra(Intent.EXTRA_EMAIL,
new String[] { toEmail });
send.putExtra(Intent.EXTRA_SUBJECT, subject);
send.putExtra(Intent.EXTRA_TEXT, message);
return Intent.createChooser(send, "Your Title Here");
}
This is quoted from Android official doc, I've tested it on Android 4.4, and works perfectly. See more examples at https://developer.android.com/guide/components/intents-common.html#Email
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);
}
}
Replace
i.setType("text/plain");
with
// need this to prompts email client only
i.setType("message/rfc822");
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","opinions#gmail.com.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "IndiaTV News - Mobile App Feedback");
emailIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(Settings.this.getString(R.string.MailContent)));
startActivityForResult(Intent.createChooser(emailIntent, "Send email..."),0);

How to send emails from my Android application?

I am developing an application in Android. I don't know how to send an email from the application?
The best (and easiest) way is to use an Intent:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Otherwise you'll have to write your own client.
Use .setType("message/rfc822") or the chooser will show you all of the (many) applications that support the send intent.
I've been using this since long time ago and it seems good, no non-email apps showing up. Just another way to send a send email intent:
Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
intent.setData(Uri.parse("mailto:default#example.com")); // or just "mailto:" for blank
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
I was using something along the lines of the currently accepted answer in order to send emails with an attached binary error log file. GMail and K-9 send it just fine and it also arrives fine on my mail server. The only problem was my mail client of choice Thunderbird which had troubles with opening / saving the attached log file. In fact it simply didn't save the file at all without complaining.
I took a look at one of these mail's source codes and noticed that the log file attachment had (understandably) the mime type message/rfc822. Of course that attachment is not an attached email. But Thunderbird cannot cope with that tiny error gracefully. So that was kind of a bummer.
After a bit of research and experimenting I came up with the following solution:
public Intent createEmailOnlyChooserIntent(Intent source,
CharSequence chooserTitle) {
Stack<Intent> intents = new Stack<Intent>();
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
"info#example.com", null));
List<ResolveInfo> activities = getPackageManager()
.queryIntentActivities(i, 0);
for(ResolveInfo ri : activities) {
Intent target = new Intent(source);
target.setPackage(ri.activityInfo.packageName);
intents.add(target);
}
if(!intents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(intents.remove(0),
chooserTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new Parcelable[intents.size()]));
return chooserIntent;
} else {
return Intent.createChooser(source, chooserTitle);
}
}
It can be used as follows:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("*/*");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(crashLogFile));
i.putExtra(Intent.EXTRA_EMAIL, new String[] {
ANDROID_SUPPORT_EMAIL
});
i.putExtra(Intent.EXTRA_SUBJECT, "Crash report");
i.putExtra(Intent.EXTRA_TEXT, "Some crash report details");
startActivity(createEmailOnlyChooserIntent(i, "Send via email"));
As you can see, the createEmailOnlyChooserIntent method can be easily fed with the correct intent and the correct mime type.
It then goes through the list of available activities that respond to an ACTION_SENDTO mailto protocol intent (which are email apps only) and constructs a chooser based on that list of activities and the original ACTION_SEND intent with the correct mime type.
Another advantage is that Skype is not listed anymore (which happens to respond to the rfc822 mime type).
To JUST LET EMAIL APPS to resolve your intent you need to specify ACTION_SENDTO as Action and mailto as Data.
private void sendEmail(){
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:" + "recipient#example.com")); // You can use "mailto:" if you don't know the address beforehand.
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "My email's body");
try {
startActivity(Intent.createChooser(emailIntent, "Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Activity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
}
}
The solution to this is simple: the android documentation explains it.
(https://developer.android.com/guide/components/intents-common.html#Email)
The most important is the flag: it is ACTION_SENDTO, and not ACTION_SEND
The other important line is
intent.setData(Uri.parse("mailto:")); ***// only email apps should handle this***
By the way, if you send an empty Extra, the if() at the end won't work and the app won't launch the email client.
According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:
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);
}
}
The strategy of using .setType("message/rfc822") or ACTION_SEND seems to also match apps that aren't email clients, such as Android Beam and Bluetooth.
Using ACTION_SENDTO and a mailto: URI seems to work perfectly, and is recommended in the developer documentation. However, if you do this on the official emulators and there aren't any email accounts set up (or there aren't any mail clients), you get the following error:
Unsupported action
That action is not currently supported.
As shown below:
It turns out that the emulators resolve the intent to an activity called com.android.fallback.Fallback, which displays the above message. Apparently this is by design.
If you want your app to circumvent this so it also works correctly on the official emulators, you can check for it before trying to send the email:
private void sendEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO)
.setData(new Uri.Builder().scheme("mailto").build())
.putExtra(Intent.EXTRA_EMAIL, new String[]{ "John Smith <johnsmith#yourdomain.com>" })
.putExtra(Intent.EXTRA_SUBJECT, "Email subject")
.putExtra(Intent.EXTRA_TEXT, "Email body")
;
ComponentName emailApp = intent.resolveActivity(getPackageManager());
ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
if (emailApp != null && !emailApp.equals(unsupportedAction))
try {
// Needed to customise the chooser dialog title since it might default to "Share with"
// Note that the chooser will still be skipped if only one app is matched
Intent chooser = Intent.createChooser(intent, "Send email with");
startActivity(chooser);
return;
}
catch (ActivityNotFoundException ignored) {
}
Toast
.makeText(this, "Couldn't find an email app and account", Toast.LENGTH_LONG)
.show();
}
Find more info in the developer documentation.
Sending email can be done with Intents which will require no configuration. But then it will require user interaction and the layout will be a bit restricted.
Build and sending a more complex email without user interaction entails building your own client. The first thing is that the Sun Java API for email are unavailable. I have had success leveraging the Apache Mime4j library to build email. All based on the docs at nilvec.
Here is the sample working code which opens mail application in android device and auto-filled with To address and Subject in the composing mail.
protected void sendEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:feedback#gmail.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
I use the below code in my apps. This shows exactly email client apps, such as Gmail.
Intent contactIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", getString(R.string.email_to), null));
contactIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject));
startActivity(Intent.createChooser(contactIntent, getString(R.string.email_chooser)));
This will show you only the email clients (as well as PayPal for some unknown reason)
public void composeEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hi#example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
try {
startActivity(Intent.createChooser(intent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
This is how I did it. Nice and simple.
String emailUrl = "mailto:email#example.com?subject=Subject Text&body=Body Text";
Intent request = new Intent(Intent.ACTION_VIEW);
request.setData(Uri.parse(emailUrl));
startActivity(request);
I used this code to send mail by launching default mail app compose section directly.
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setType("message/rfc822");
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"test#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
This function first direct intent gmail for sending email, if gmail is not found then promote intent chooser. I used this function in many commercial app and it's working fine. Hope it will help you:
public static void sentEmail(Context mContext, String[] addresses, String subject, String body) {
try {
Intent sendIntentGmail = new Intent(Intent.ACTION_VIEW);
sendIntentGmail.setType("plain/text");
sendIntentGmail.setData(Uri.parse(TextUtils.join(",", addresses)));
sendIntentGmail.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntentGmail.putExtra(Intent.EXTRA_EMAIL, addresses);
if (subject != null) sendIntentGmail.putExtra(Intent.EXTRA_SUBJECT, subject);
if (body != null) sendIntentGmail.putExtra(Intent.EXTRA_TEXT, body);
mContext.startActivity(sendIntentGmail);
} catch (Exception e) {
//When Gmail App is not installed or disable
Intent sendIntentIfGmailFail = new Intent(Intent.ACTION_SEND);
sendIntentIfGmailFail.setType("*/*");
sendIntentIfGmailFail.putExtra(Intent.EXTRA_EMAIL, addresses);
if (subject != null) sendIntentIfGmailFail.putExtra(Intent.EXTRA_SUBJECT, subject);
if (body != null) sendIntentIfGmailFail.putExtra(Intent.EXTRA_TEXT, body);
if (sendIntentIfGmailFail.resolveActivity(mContext.getPackageManager()) != null) {
mContext.startActivity(sendIntentIfGmailFail);
}
}
}
This is the most clean way of sending email on Android.
val intent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf("email#example.com"))
putExtra(Intent.EXTRA_SUBJECT, "Subject")
putExtra(Intent.EXTRA_TEXT, "Email body")
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
You also need to specify in your manifest (outside your application tag) the query for applications that handle email (mailto)
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
If you need to send HTML text in the email body, please replace the "Email body" with your email string, something like this (please beware that Html.fromHtml maybe deprecated this was only for show you how to do it)
Html.fromHtml(
StringBuilder().append("<b>Hello world</b>").toString()
)
simple try this one
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
String message = textMessage.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
// email.putExtra(Intent.EXTRA_CC, new String[]{ to});
// email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
// need this to prompts email client only
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
});
}
Other solution can be
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.setType("plain/text");
emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"someone#gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Yo");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hi");
startActivity(emailIntent);
Assuming most of the android device has GMail app already installed.
Use this for send email...
boolean success = EmailIntentBuilder.from(activity)
.to("support#example.org")
.cc("developer#example.org")
.subject("Error report")
.body(buildErrorReport())
.start();
use build gradle :
compile 'de.cketti.mailto:email-intent-builder:1.0.0'
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","ebgsoldier#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Forgot Password");
emailIntent.putExtra(Intent.EXTRA_TEXT, "this is a text ");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
This method work for me. It open Gmail app (if installed) and set mailto.
public void openGmail(Activity activity) {
Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setType("text/plain");
emailIntent.setType("message/rfc822");
emailIntent.setData(Uri.parse("mailto:"+activity.getString(R.string.mail_to)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.app_name) + " - info ");
final PackageManager pm = activity.getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
best = info;
if (best != null)
emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
activity.startActivity(emailIntent);
}
/**
* Will start the chosen Email app
*
* #param context current component context.
* #param emails Emails you would like to send to.
* #param subject The subject that will be used in the Email app.
* #param forceGmail True - if you want to open Gmail app, False otherwise. If the Gmail
* app is not installed on this device a chooser will be shown.
*/
public static void sendEmail(Context context, String[] emails, String subject, boolean forceGmail) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL, emails);
i.putExtra(Intent.EXTRA_SUBJECT, subject);
if (forceGmail && isPackageInstalled(context, "com.google.android.gm")) {
i.setPackage("com.google.android.gm");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else {
try {
context.startActivity(Intent.createChooser(i, "Send mail..."));
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No email app is installed on your device...", Toast.LENGTH_SHORT).show();
}
}
}
/**
* Check if the given app is installed on this devuice.
*
* #param context current component context.
* #param packageName The package name you would like to check.
* #return True if this package exist, otherwise False.
*/
public static boolean isPackageInstalled(#NonNull Context context, #NonNull String packageName) {
PackageManager pm = context.getPackageManager();
if (pm != null) {
try {
pm.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return false;
}
Try this:
String mailto = "mailto:bob#example.org" +
"?cc=" + "alice#example.com" +
"&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(bodyText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
try {
startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
//TODO: Handle case where no email app is available
}
The above code will open the users favourite email client prefilled with the email ready to send.
Source
The code below works on Android 10 devices and higher. It also sets the subject, body and recipient(To).
val uri = Uri.parse("mailto:$EMAIL")
.buildUpon()
.appendQueryParameter("subject", "App Feedback")
.appendQueryParameter("body", "Body Text")
.appendQueryParameter("to", EMAIL)
.build()
val emailIntent = Intent(Intent.ACTION_SENDTO, uri)
startActivity(Intent.createChooser(emailIntent, "Select app"))
Kotlin version which only shows Email clients (no contacts etc.):
with(Intent(Intent.ACTION_SEND)) {
type = "message/rfc822"
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf("user#example.com"))
putExtra(Intent.EXTRA_SUBJECT,"YOUR SUBJECT")
putExtra(Intent.EXTRA_TEXT, "YOUR BODY")
try {
startActivity(Intent.createChooser(this, "Send Email with"))
} catch (ex: ActivityNotFoundException) {
// No email clients found, might show Toast here
}
}
import androidx.core.app.ShareCompat
import androidx.core.content.IntentCompat
ShareCompat.IntentBuilder(this)
.setType("message/rfc822")
.setEmailTo(arrayOf(email))
.setStream(uri)
.setSubject(subject)
.setText(message + emailMessage)
.startChooser()
Filtering for 'real' E-Mail Apps is still an issue today. As many people mentioned above, other apps nowadays also report to support the mime-type "message/rfc822". Therefore, this mime-type is not suitable any more to filter for a real E-Mail App.
If you want to send a simple text mail, it is enough to use the the ACTION_SENDTO intent action with the appropriate data type like so:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, text);
Intent chooser = Intent.createChooser(intent, "Send Mail");
context.startActivity(chooser);
This will filter all available apps for those that support the 'mailto' protocol, which is much more suitable for the purpose of sending an E-mail.
But sadly things become complicated, if you want to send a mail with (multiple) attachments. The ACTION_SENDTO action does not support the EXTRA_STREAM extra on the intent. If you want to use that, you must use the ACTION_SEND_MULTIPLE action, which does not work together with the the data type Uri.parse("mailto:").
For now I found a solution, which consists of the following steps:
Declare that your App wants to query Apps on the device, that support the mailto protocol (important for all Apps since Android 11)
Actually query all Apps, that support the mailto protocol
For each supporting App: Build the intent that you actually want to launch, aiming for that single App
Build the App chooser and launch it
And this is how it looks in code:
Add this to the AndroidManifest:
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
This is the Java code:
/* Query all Apps that support the 'mailto' protocol */
PackageManager pm = context.getPackageManager();
Intent emailCheckerIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
List<ResolveInfo> emailApps = pm.queryIntentActivities(emailCheckerIntent, PackageManager.MATCH_DEFAULT_ONLY);
/* For each supporting App: Build an intent with the desired values */
List<Intent> intentList = new ArrayList<>();
for (ResolveInfo resolveInfo : emailApps) {
String packageName = resolveInfo.activityInfo.packageName;
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setPackage(packageName);
intent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.putExtra(Intent.EXTRA_STREAM, attachmentUris);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //IMPORTANT to give the E-Mail App access to your attached files
intentList.add(intent);
}
/* Create a chooser consisting of the queried apps only */
Intent chooser = Intent.createChooser(intentList.remove(intentList.size() - 1), "Send Mail");
Intent[] extraIntents = intentList.toArray(new Intent[0]);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
context.startActivity(chooser);
Note: If the itentList only has one item, Android will automatically skip the chooser and run the only App automatically.

Categories

Resources