Sending data with multiple attachments with email intent - android

I have a FeedbackActivity.java activity which takes feedback from user with multiple attachments (upto 3 images as attachments).
I am using following code:
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, emails); //emails is an Array of 'String' type
intent.putExtra(Intent.EXTRA_SUBJECT, subject); //subject is a String
intent.putExtra(Intent.EXTRA_TEXT, text) //text is a String
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); //uris is an ArrayList of 'Uri' type
//uris stores all Uri of images selected
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
else {
Toast.makeText(this, "Not Good", Toast.LENGTH_SHORT).show();
}
Now this code works fine but the problem is that it shows all sorts of apps which support "message/rfc822" MIME type.
Image is shown below :
I only need to show the email client apps, I tried Uri.parse("mailto:"), but didn't workout and code always moves to else statement and shows the toast "not good".
I read the google documentation but it only shows simple cases.
I tried searching on the web. Many developers are using intent.setType("*/*") or intent.setType("text/plain"). But they all too show apps other than email clients.
Please guide me.
And I wanted to ask in general,
Google documentations show simple examples which is good in a way, but how to learn really in depth on these kind of topics?
Thank you.

So here, we will be using two intents: selectorIntent and emailIntent. selectorIntent is what the emailIntent will use as to show available apps. code:
Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emails);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
emailIntent.setSelector(selectorIntent);
if(emailIntent.resolveActivity(getPackageManager()) != null){
startActivity(emailIntent);
}
else {
Snackbar.make(scrollView, "Sorry, We couldn't find any email client apps!", Snackbar.LENGTH_SHORT).show();
}
Now it will choose only apps which are email client.
If there is only one email-client app in your phone than it will directly open that. And if no such application is there, than the code will show Snackbar given in the else part.

Don't use Uri.parse, use Uri.fromParts
Do it like this:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","example#mail.com", null));

Related

Send an email while showing only email programs

I have multiple posts about sending an email and even in the Android documentations. IT does not seem to work. I want to basically to allow the user to choose their email programs to send an email. Here is the code
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("*/*");
emailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{"xyz#gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT , "msg");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
However it does nothing (ie no activity)
What is it that I am doing wrong? If I use Action.Send then it display all social medial platforms including facebook and whatsapp ..etc
Thank you
This is taken from Adroid developers site and combining with this question:
public void composeEmail(String[] addresses, String subject, String msessage) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Note I made a few changes to match the case of the question.

How to export and import SQLite Database from Android Studio? [duplicate]

I created my own file extension (.oli). If the user clicks on a file with this extension my app starts and loads the included data. This works like expected. The problem is I would like to give the user of my app the opportunity to share a file (Example: filename.oli).
I implemented this so far:
public void shareFile(){
File file = getShareableFile(); //Creates a .oli-file
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri uri = Uri.fromFile(file);
shareIntent.setType("*/*"); //Maybe the problem
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, name);
startActivity(Intent.createChooser(shareIntent, getString(R.string.shareDatei)));
}
The problem is, the list of apps that think they could handle sharing my file is very large because of shareIntent.setType("/"); Here are two cases that happen if you share with different apps:
If I choose an Email-app like Gmail to share my file it works how it should. The email includes the file as filename.oli . When I click on it my app gets started.
But If I choose for example the Quickmemo-app I get a message that this file can not be shared by this.
So all in all I just want to show apps in the chooserlist that can handle to share my file with the .oli extension . How do I do that? Thanks in advance!
The setType() intent method actually uses MIME types to filter apps according to the docs. So, you'll only be able to filter the apps available based on the following filters:
1.Text
sendIntent.setType("text/plain");
2. Binary
shareIntent.setType("image/jpeg");
3. Multiple content items
shareIntent.setType("image/*");
EDIT
Here's what I would likely do, I would know which apps I want to be able to share the file, like gmail since you know that works, and I would be selective about which apps are in the list. The following code comes from the answer in this SO link: How to filter specific apps for ACTION_SEND intent (and set a different text for each app)
public void onShareClick(View v) {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
} else if(packageName.contains("facebook")) {
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
} else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}

Android Studio: Email fill on gmail?

I've integrated code to send an email from my app.i have search on that and found the solution which i have already integrated but it didn't work for me.
Basically, The code include the text and the subject but does not add the email address (on which we have to send email) in gmail.
Can any one help me?
protected void sendEmail (String strtwi){
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, "projectmyangel#hotmail.com");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Spread More Cheer!");
emailIntent.putExtra(Intent.EXTRA_TEXT, strtwi);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Email"));
}
change this line
emailIntent.putExtra(Intent.EXTRA_EMAIL, "projectmyangel#hotmail.com");
with
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"myEmail#gmail.com"});
if above didn't solve your issue try below:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"myEmail#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(intent, "Contact Me..."));
}
Android API does not supply the assignment of the email address (on which to send mail).
The intent is just a message that sent to the application you select in Intent.createChooser(emailIntent, "Email") and fill in the necessary columns refer to the information you have given.
Since the email address that the mail sent from differs from one user to another, it is better to choose them in the email application the user select rather than in your application.

Android Email Multiple Attachment issue on HTC Thunderbolt

I have a weird situation here.
I am trying to send emails with multiple attachments using the following piece of code.
Intent emailIntent = new Intent( android.content.Intent.ACTION_SEND_MULTIPLE );
// emailIntent.setType( "plain/text" );
emailIntent.setType( "application/octet-stream" );
...
....
emailIntent.putParcelableArrayListExtra( Intent.EXTRA_STREAM, uris );
This works fine and the implicit intent mechanism shows up a lot of options like Gmail, Skype, Messaging etc.
The problem is that the default Mail client does not show up on HTC Thunderbolt ( but works on other devices including HTC Incredible S ).
If I try to send a single attachment using Intent.ACTION_SEND, the default mail client shows up. I have tried setting content type to text/plain, appliation/octet-stream, message/rfc282 etc but none works.
What am I missing here?
I had the same issue, I Fixed it with using http Mime Library for multipart form entity.
here is the link to the file.
http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/apidocs/org/apache/http/entity/mime/HttpMultipart.html
Sounds like a bug in the Thunderbolt's version of Sense. Custom UIs for the win, am I right?
Anyway, I would look up what app actually does handle emails on the thunderbolt and put an if-statement to detect if the device is a thunderbolt. If it is, set the Intent's target class to whatever that is. If it's not, do what you're already doing.
This works great for me, be sure to specify the the message type, that is how the android os knows which broadcast to use.
String email = "test#email.com";
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {email}); // could have multiple address
intent.putExtra(Intent.EXTRA_SUBJECT, "Enter your subject here");
intent.putExtra(Intent.EXTRA_TEXT, "message text as needed");
ArrayList<Uri> arrayUri = new ArrayList<Uri>();
arrayUri.add(Uri.parse("file://" + paths[0]));
arrayUri.add(Uri.parse("file://" + paths[1]));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
startActivity(Intent.createChooser(intent, "Any title to show on chooser"));
Try this. I think it will work.
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");
ArrayList<Uri> uris = new ArrayList<Uri>();
String[] filePaths = new String[] {image1 Path,image2 path};
for (String file : filePaths) {
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
if ( !(app_preferences.getString("email", "") == null || app_preferences.getString("email", "").equals(""))) {
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {app_preferences.getString("email", "")});
}
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject name");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Please find the attachment.");
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Email:"));

Android Button onclick submit to email

I have been working with Java and Xml for a few months now, and have learned a great deal thanks to everyones help on StackOverflow.
My question is about java programming for android in relation to the submit button.
Currently I am trying to figure out how to submit a value to an email address (behind the scenes)
Lets say we have a text field and a button; I want to take the value entered in the text field, and submit that to an email address onclick.
I am unable to find anything online that shows me how to do this.
Thank you in advance for reading through my post and I look forward to your suggestions.
This is a great example of how using Intents can come in great handy!
Android has a bunch of pre-defined Intents that do certain things within the system; you may have clicked on a picture before and a dialog popped up asking whether you would like to view it in your gallery or in a third-party app such as Astro. The viewing of an image has its own pre-determined intent.
Sending an email also has its own pre-determined intent: android.content.Intent.ACTION_SEND. You'll need to create an intent with that property and then attach extra information (ie. the address to send to, the subject/message body, etc.).
Example code:
// Data members
private Intent emailIntent;
private String feedback;
private EditText feedbackBox;
// Create the Intent, and give it the pre-defined value
// that the Android machine automatically associates with
// sending an email.
emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
// Put extra information into the Intent, including the email address
// that you wish to send to, and any subject (optional, of course).
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"your_email#whatever.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Insert subject here");
// Acquire feedback from an EditText and save it to a String.
feedback = feedbackBox.getText().toString();
// Put the message into the Intent as more extra information,
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, feedback);
// Start the Intent, which will launch the user's email
// app (make sure you save any necessary information in YOUR app
// in your onPause() method, as launching the email Intent will
// pause your app). This will create what I discussed above - a
// popup box that the user can use to determine which app they would like
// to use in order to send the email.
startActivity(Intent.createChooser(emailIntent, "Insert title for dialog box."));
I hoped this helped!!
Some sources you might like to check out:
http://developer.android.com/guide/topics/intents/intents-filters.html
http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, someEditText.getText());
startActivity(Intent.createChooser(emailIntent, "Send someone an email..."));
try this
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:Type email address here"));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
It works good
String mail=mailid.getText().toString();
String msubject=subject.getText().toString();
String mbody=body.getText().toString();
Log.i("Send email", "");
String[] TO = {mail};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Static subject "+ msubject);
emailIntent.putExtra(Intent.EXTRA_TEXT, "Static body "+ mbody);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}

Categories

Resources