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"));
Related
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.
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"));
I want to send the email from my android app. If i m using
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
It is opening skype,bluetooth along with the mail client
and if i use
Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
then it is only opening mail client,but in the text body it is adding %20%20 when there is new line
Which approach is suitable to get only mail client and body of the mail containing new lines and spaces.
Try sending it as below:
String subject = "mail subject";
String body = "whatever the mail content is. may include html tags too";
Intent intMail = new Intent(Intent.ACTION_SEND);
intMail.setType("message/rfc822");
intMail.putExtra(Intent.EXTRA_SUBJECT, subject);
intMail.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
startActivity(Intent.createChooser(intMail, "Send Email..."));
Try to use this code. You can keep subject, body etc as optional. It only opens email client and gives right output.
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Body);
startActivity(Intent.createChooser(emailIntent, "Email:"));
every time i create an action for sending an email from my app, it prompts to many options including a QR client...
Is there a way to force sending via email clients only?
Code for sending the email
String rec[] = { owner.email };
i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_EMAIL, rec);
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "RE: " + desc);
i.putExtra(android.content.Intent.EXTRA_TEXT,
"\n\n\nSent from Mojo for Android");
startActivity(i);
Screenshot for what happens when I launch this
Try to setType message/rfc822 instead of text/plain
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "abc#xyz.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject of the Mail");
emailIntent.putExtra( android.content.Intent.EXTRA_TEXT,
"This is my sample Mail");
emailIntent.setType("vnd.android.cursor.dir/email");
startActivity(Intent.createChooser(emailIntent, "Email:"));
else use this it will shows only the mail clients,
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "abc#xyz.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject of the Mail");
emailIntent.putExtra( android.content.Intent.EXTRA_TEXT,
"This is my sample Mail");
//emailIntent.setType("vnd.android.cursor.dir/email");
startActivity(Intent.createChooser(emailIntent, "Email:"));
I think you should change the setType to
i.setType("message/rfc822") ;
Even though it's too late for #thepoosh, but it may be helpful for future questioners. After a lot of searching and testing, I finally found a good solution.
Thanks to the Open source developer, cketti for sharing his/her concise 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
}
This is the link to his/her gist.
It will show all the available app installed on android phone which can perform sharing or send a link from webview to others. Like - Gmail, facebook, imo, whatsapp, messenger etc.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String shareLink = webView.getUrl();
intent.putExtra(Intent.EXTRA_TEXT, shareLink);
startActivity(Intent.createChooser(intent, "Share via..."));
But when you force to open mail app only :
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"something#gmail.com"});
try {
startActivity(Intent.createChooser(intent, "send mail"));
} catch (ActivityNotFoundException ex) {
Toast.makeText(this, "No mail app found!!!", Toast.LENGTH_SHORT);
} catch (Exception ex) {
Toast.makeText(this, "Unexpected Error!!!", Toast.LENGTH_SHORT);
}
As long as you are using ACTION_SEND with type text/plain, it will show all the valid options. However, if you want, you may design your your own dialog window which shows only Gmail or other mail client by doing filtering programatically.
BTW, why do you even need this window when you just want to use Gmail?
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:?to=email&subject=hello&body=hello%20world"));
startActivity(Intent.createChooser(intent, "Send via..."));
you can try this:::::
Intent.setType("plain/text");
At first when I spotted this I immediately though it was a mistake and it was meant to be text/plain, but this is actually the correct way to only display E-mail clients in the application list.
Give it a try and see for yourself.
intent.setPackage("com.google.android.gm"); //Added Gmail Package to forcefully open Gmail App
String rec[] = { owner.email };
i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822") ;
i.putExtra(android.content.Intent.EXTRA_EMAIL, rec);
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "RE: " + desc);
i.putExtra(android.content.Intent.EXTRA_TEXT,
"\n\n\nSent from Mojo for Android");
startActivity(i);
try this;:::
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);