I want to send the photo by only email using Intent. I am using below code but its not opening only gmail but showing many share options.
Please help me to share the only gmail.
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg"); // put here your mime type
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if(!resInfo.isEmpty()) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
ArrayList<Uri> uris = new ArrayList<Uri>();
for (ResolveInfo info : resInfo) {
if(info.activityInfo.packageName.toLowerCase().contains("gmail") || info.activityInfo.name.toLowerCase().contains("gmail")) {
targetedShare.setType("image/jpeg"); // put here your mime type
targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Amplimesh Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT,"Attached the Quote");
//Fetching the Installed App and open the Gmail App.
for(int index = 0; index < productList.size(); index++) {
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(productList.get(index).getOverlayBitmap());
Bitmap overLayBitmap = BitmapFactory.decodeStream(byteInputStream);
String fileName = SystemClock.currentThreadTimeMillis() + ".png";
//Save the bitmap to cache.
boolean isSaved = Helper.saveImageToExternalStorage(overLayBitmap, getApplicationContext(), fileName);
if(isSaved)
uris.add(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/amplimesh/images/" + fileName)));
}
}
}
targetedShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(targetedShare, "Sending multiple attachment"), 12345);
}
You can not get only gmail. but you can target some content type application.
try this
intent.setType("message/rfc822");
Try this:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/jpeg");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {""});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EMAIL_SUBJECT);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, EMAIL_BODY);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+fileName));
startActivity(Intent.createChooser(emailIntent, "Sharing Options"));
For me its working..
try Intent.ACTION_SENDTO
This is the method.
public void emailShare()
{
Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
emailIntent.setType("image/jpeg");
//File bitmapFile = new File(Environment.getExternalStorageDirectory()+"DCIM/Camera/img.jpg");
//myUri = Uri.fromFile(bitmapFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/DCIM/Camera/img.jpg"));
emailIntent.setData(Uri.parse("mailto:"));
startActivityForResult(Intent.createChooser(emailIntent, "Complete action using:"),PICK_CONTACT_REQUEST);
}
Where startActivityForResult is to get the result back. and MESSAGE_RESULT is the result expected if mail sent successfully.
Catch the result on
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == MESSAGE_RESULT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "E-Mail sent successfully", Toast.LENGTH_LONG).show();
}
}
}
Declare static final int MESSAGE_RESULT = 1; at the begining.
Hope it helps.
use Intent.ACTION_VIEW insted of Intent.ACTION_SEND
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "Subject" + "&body=" + "Body" + "&to=" + "email#mail.com");
intent.setData(data);
startActivity(Intent.createChooser(intent, "Choose app"));
or you can use:
startActivity(intent);
String shareImageLocation="Image file address";//Give file address here
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { "someone#someone.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "Send photos");
i.putExtra(Intent.EXTRA_STREAM, shareImageLocation);
String bugReportBody = description;
i.putExtra(Intent.EXTRA_TEXT, bugReportBody);
ArrayList<Uri> uris = new ArrayList<Uri>();
File fileIn = new File(shareImageLocation);
uris.add(Uri.fromFile(fileIn));
i.putExtra(Intent.EXTRA_STREAM, uris);
try {
startActivityForResult(
Intent.createChooser(i, "Complete action using"),
SEND_EMAIL);
} catch (android.content.ActivityNotFoundException ex) {
}
Using only gmail app is not a good idea. Because there lot of phones which doesn't have gmail app installed. Try this code which will show all the app which can send email. I'm sure it won't show all the sharing app in your phone. But it will show some others apps which can handle any sharing intent. Like google drive.
Related
Is there any way to customize the createChooser of share intent in startActivity(Intent.createChooser(i,"Share via"));?
Showing the apps not in a dialogue box maybe in scroll view as buttons
String urlToShare = "www.google.com"
code to share link twitter
try {
Intent shareIntent = ShareCompat.IntentBuilder.from(getParent())
.setType("text/plain")
.setText("Sharing text with image link \n "+urlToShare).setStream(null)
.getIntent()
.setPackage("com.twitter.android");
startActivity(shareIntent);
} catch (Exception e) {
// If we failed (not native FB app installed), try share through SEND
Intent intent = new Intent(Intent.ACTION_SEND);
String sharerUrl = "https://twitter.com/intent/tweet?text=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(intent);
}
code to share link google plus
try {
Intent shareIntent = ShareCompat.IntentBuilder.from(getParent())
.setType("text/plain")
.setText("Sharing text with image link \n "+urlToShare).setStream(null)
.getIntent()
.setPackage("com.google.android.apps.plus");
startActivity(shareIntent);
} catch (Exception e) {
// If we failed (not native FB app installed), try share through SEND
Intent intent = new Intent(Intent.ACTION_SEND);
String sharerUrl = "https://plus.google.com/share?url=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(intent);
}
code to share link whatsapp
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
startActivity(intent);
Yes you can do it by getting the share options as activities and pass them to your adapter, i am posting a sample code
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List activities = this.getPackageManager().queryIntentActivities(sendIntent, 0);
By this you will get share Intents in activities object and then you can convert it to Object Array some thing like this
Objects[] item = activities.toArray();
for( int i=0; i<item.length; i++ ) {
ResolveInfo infoName = (ResolveInfo) items[i];
String name = info.activityInfo.name;
Drawable logo = info.loadIcon(context.getPackageManager());
// Set them to your Views
}
and when your view is clicked and you want to perform the share functionality you will do some thing like this
ResolveInfo info = (ResolveInfo) item(position);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Your Text");
startActivity(intent);
this is what i have done to share link and image to another apps something like this just try
/*
* Method to share either text or URL.
*/
private void shareTextUrl() {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, "http://www.google.com");
startActivity(Intent.createChooser(share, "Share link!"));
}
/*
* Method to share any image.
*/
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
I want to share my image from app only using gmail.
Here is my piece of code:
if (id == R.id.menu_item_share) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_EMAIL,`enter code here`
new String[] { "bipush.osti#gmail.com" });
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "compose mail");
sharingIntent.putExtra(Intent.EXTRA_STREAM, targetUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
How do I modify my codes
This method allow you to share the data for a single application.
public void share(String nameApp, String imagePath, String message) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_EMAIL,`enter code here`
new String[] { "bipush.osti#gmail.com" });
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "compose mail");
List<ResolveInfo> resInfo = getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your mime
// type
if (info.activityInfo.packageName.toLowerCase().contains(
nameApp)
|| info.activityInfo.name.toLowerCase().contains(
nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT, message);
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(imagePath)));
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
} catch (Exception e) {
Log.v("VM",
"Exception while sending image on" + nameApp + " "
+ e.getMessage());
}
}
For example you can share the image for the gmail by using.
File filePath = new File("your image path");
share("gmail",filePath.toString(),"your comment");
you can also share to other application by simply changing the name.
share("facebook",filePath.toString(),"your comment");
share("twitter",filePath.toString(),"your comment");
Try to setType message/rfc822 instead of image/png
You have to mention the package name of the application which you want to handle it. To send it through the Gmail app add the below line
sharingIntent.setPackage("com.google.android.gm");
I have an action_send intent that allows the user to send an email however when they are done I would like to return them to a different activity than they were on when the email was sent.
My intent code is below :
public void sendEmail(){
String path = Environment.getExternalStorageDirectory().toString() + "/" + "screenshots/";
String target_filename = "CheeseNav.jpg";
File externalFile = new File(path, target_filename);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from android app");
i.putExtra(Intent.EXTRA_TEXT, "");
Uri sendUri = Uri.fromFile(externalFile);
i.putExtra(Intent.EXTRA_STREAM, sendUri);
try{
startActivity(Intent.createChooser(i, "Send mail..."));
} catch(android.content.ActivityNotFoundException ex){
Toast.makeText(ScreenViewer.this, "There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
}
If you want to return to some activity had before the process to send the email you have to make an intent with flags to clear the top of the activities. You can use Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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);
I have a problem .. I want only email activities to resolve intent ACTION.SEND but beside email I get other apps as well (e.g TubeMate) even though I have set the mime type as 'message/rfc822' ... Any idea how can I get Email applications to resolve it ..
String recepientEmail = ""; // either set to destination email or leave empty
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recepientEmail));
startActivity(intent);
The point is to use ACTION_SENDTO as action and mailto: as data. If you want to let the user specify the destination email, use just mailto:; if you specify email yourself, use mailto:name#example.com
Suggested method filters all the application, that can send email(such as default email app or gmail)
Here is how I send email with attachments (proved to work with attachments of various MIME types, even in the same email) and only allow email apps (it also has a solution for cases that no app support "mailto").
At first, we try and get activities that support mailto: format. If none are found then we get all activities that supports the message/rfc822 MIME type.
We select the default app (if there is a default) or allow the user to select from the available apps.
If no app supports mailto: and message/rfc822, then we use the default chooser.
public static void sendEmail(final Context p_context, final String p_subject, final String p_body, final ArrayList<String> p_attachments)
{
try
{
PackageManager pm = p_context.getPackageManager();
ResolveInfo selectedEmailActivity = null;
Intent emailDummyIntent = new Intent(Intent.ACTION_SENDTO);
emailDummyIntent.setData(Uri.parse("mailto:some#example.com"));
List<ResolveInfo> emailActivities = pm.queryIntentActivities(emailDummyIntent, 0);
if (null == emailActivities || emailActivities.size() == 0)
{
Intent emailDummyIntentRFC822 = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailDummyIntentRFC822.setType("message/rfc822");
emailActivities = pm.queryIntentActivities(emailDummyIntentRFC822, 0);
}
if (null != emailActivities)
{
if (emailActivities.size() == 1)
{
selectedEmailActivity = emailActivities.get(0);
}
else
{
for (ResolveInfo currAvailableEmailActivity : emailActivities)
{
if (true == currAvailableEmailActivity.isDefault)
{
selectedEmailActivity = currAvailableEmailActivity;
}
}
}
if (null != selectedEmailActivity)
{
// Send email using the only/default email activity
sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, selectedEmailActivity);
}
else
{
final List<ResolveInfo> emailActivitiesForDialog = emailActivities;
String[] availableEmailAppsName = new String[emailActivitiesForDialog.size()];
for (int i = 0; i < emailActivitiesForDialog.size(); i++)
{
availableEmailAppsName[i] = emailActivitiesForDialog.get(i).activityInfo.applicationInfo.loadLabel(pm).toString();
}
AlertDialog.Builder builder = new AlertDialog.Builder(p_context);
builder.setTitle(R.string.select_mail_application_title);
builder.setItems(availableEmailAppsName, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, emailActivitiesForDialog.get(which));
}
});
builder.create().show();
}
}
else
{
sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, null);
}
}
catch (Exception ex)
{
Log.e(TAG, "Can't send email", ex);
}
}
protected static void sendEmailUsingSelectedEmailApp(Context p_context, String p_subject, String p_body, ArrayList<String> p_attachments, ResolveInfo p_selectedEmailApp)
{
try
{
Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
String aEmailList[] = { "some#example.com"};
emailIntent.putExtra(Intent.EXTRA_EMAIL, aEmailList);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, null != p_subject ? p_subject : "");
emailIntent.putExtra(Intent.EXTRA_TEXT, null != p_body ? p_body : "");
if (null != p_attachments && p_attachments.size() > 0)
{
ArrayList<Uri> attachmentsUris = new ArrayList<Uri>();
// Convert from paths to Android friendly Parcelable Uri's
for (String currAttachemntPath : p_attachments)
{
File fileIn = new File(currAttachemntPath);
Uri currAttachemntUri = Uri.fromFile(fileIn);
attachmentsUris.add(currAttachemntUri);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachmentsUris);
}
if (null != p_selectedEmailApp)
{
Log.d(TAG, "Sending email using " + p_selectedEmailApp);
emailIntent.setComponent(new ComponentName(p_selectedEmailApp.activityInfo.packageName, p_selectedEmailApp.activityInfo.name));
p_context.startActivity(emailIntent);
}
else
{
Intent emailAppChooser = Intent.createChooser(emailIntent, "Select Email app");
p_context.startActivity(emailAppChooser);
}
}
catch (Exception ex)
{
Log.e(TAG, "Error sending email", ex);
}
}
private void sendEmail(Connect connect) {
Intent email = new Intent(Intent.ACTION_SENDTO);
email.setData(Uri.parse("mailto:"));
email.putExtra(Intent.EXTRA_EMAIL, new String[]{connect.getEmail()});
email.putExtra(Intent.EXTRA_SUBJECT, "");
email.putExtra(Intent.EXTRA_TEXT, "");
try {
startActivity(Intent.createChooser(email, getString(R.string.choose_email_client)));
} catch (ActivityNotFoundException activityNotFoundException) {
UIUtils.showShortSnackBar(fragmentConnectLayout, getString(R.string.no_email_client));
}
}
Refer https://developer.android.com/guide/components/intents-common.html#Email
In Android, there's no such thing as an email activity.
There's also no intent filter that can be created to include only email applications.
Each application (or activity) can define its own intent filters.
So when using intent ACTION_SEND, you'll have to rely on the users intelligence to pickhis favorite email app from the chooser (and not TubeMate).
u can also use:
//writes messages only to email clients
public void setWriteEmailButton() {
btnWriteMail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL , new String[]{mConsultantInfos.getConsultantEMail()});
i.putExtra(Intent.EXTRA_SUBJECT, mContext.getString(R.string.txtSubjectConsultantMail));
i.putExtra(Intent.EXTRA_TEXT , "");
try {
startActivity(Intent.createChooser(i, mContext.getString(R.string.txtWriteMailDialogTitle)));
} catch (android.content.ActivityNotFoundException ex) {
UI.showShortToastMessage(mContext, R.string.msgNoMailClientsInstalled);
}
}
});
}
have fun (combination of both ;))
This work for me to open only email apps:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:jorgesys12#gmail.com"));
startActivity(intent);
Try this
String subject = "Feedback";
String bodyText = "Enter text email";
String mailto = "mailto:bob#example.org" +
"?cc=" + "" +
"&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
}
Credit: https://medium.com/#cketti/android-sending-email-using-intents-3da63662c58f
When I use intent android.content.Intent.ACTION_SENDTO doesn't work for me because it shows many apps, some apps are not email clients. I found this way and it works perfectly for me.
Intent testIntent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "blah blah subject" + "&body=" + "blah blah body" + "&to=" + "sendme#me.com");
testIntent.setData(data);
startActivity(testIntent);
This works for me
Intent intent = new Intent("android.intent.action.SENDTO", Uri.fromParts("mailto", "yourmail#gmail.com", null));
intent.putExtra("android.intent.extra.SUBJECT", "Enter Subject Here");
startActivity(Intent.createChooser(intent, "Select an email client"));
Please check : https://developer.android.com/guide/components/intents-common#ComposeEmail
String[] sendTo = {}; // people who will receive the email
String subject = "implicit intent | sending email";
String message = "Hi, this is just a test to check implicit intent.";
Intent email = new Intent(Intent.ACTION_SENDTO);
email.setData(Uri.parse("mailto:")); // only email apps should handle this
email.putExtra(Intent.EXTRA_EMAIL, sendTo);
email.putExtra(Intent.EXTRA_SUBJECT, subject);// email subject / title
email.putExtra(Intent.EXTRA_TEXT, message);//message that you want to send
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(email, "Choose an Email client :");
// Verify the original intent will resolve to at least one activity
if (chooser.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
This is an absolutely simple and 100% working approach. Thanks to the Open source developer, cketti for sharing this concise and neat solution.
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
}
And this is the link to his/her gist.
Here's a code snippet that launches ONLY email apps.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:example#example.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "This is the subject"));
intent.putExtra(Intent.EXTRA_TEXT, "Hi, how're you doing?"));
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(intent);
}
I hope this helps.
Following three lines of code is sufficient to finish your task, nothing EXTRAS needed. Hope it helps.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:destination_gmail#gmail.com"));
startActivity(Intent.createChooser(intent, "Send email using.."));
You can also use this. It's more efficient
Intent mailIntent = new Intent(Intent.ACTION_SEND);
String mailTo[] = new String[] { "MAIL_TO_1", "MAIL_TO_2" };
mailIntent.putExtra(Intent.EXTRA_EMAIL, mailTo);
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
mailIntent.putExtra(Intent.EXTRA_TEXT, "MailsBody");
mailIntent.setType("plain/text");
startActivity(Intent.createChooser(mailIntent, "only email client"));
This code sample show only email client which are currently installed on your device