Send mail programmatically Android - android

I am using this to send email programmatically in android but in Android 4.4.4 works bad. Is there another way to do this? Here is my code, thank you.
Intent i = new Intent(Intent.ACTION_SEND);
//i.setType("text/plain"); //use this line for testing in the emulator
i.setType("message/rfc822") ; // use from live device
i.putExtra(Intent.EXTRA_SUBJECT,"-my app-");
i.putExtra(Intent.EXTRA_TEXT,"Hello");
startActivity(Intent.createChooser(i, "Select your mail app"));

The dialog appears very big on the screen
The size of the chooser window is up to the device, not you. The chooser window will be the same size for all apps on the device that trigger a chooser, and so the user will be expecting to see the "very big" chooser window on devices that have one.
If you feel that the size of the chooser window should be what you want rather than what your users will expect, you will need to create your own chooser. You can do this using PackageManager and queryIntentActivities() to see what all responds to your Intent and using that to populate some chooser UI of your own design.

I hope below code will be helpfull for you..
protected void sendEmail() {
String[] recipients = {recipient.getText().toString()};
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
// prompts email clients only
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, recipients);
email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
try {
// the user can choose the email client
startActivity(Intent.createChooser(email, "Choose an email client from..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "No email client installed.",
Toast.LENGTH_LONG).show();
}
}
Above code works for me!
Enjoy!!!

Related

Android intent not showing only email clients

So I'm trying to launch a prepopulated email client with data. The content gets populated fine, however my problem is that when launching the intent, I wanted it to only show email clients to select from.
Instead, it shows Gmail, Adding to EverNote, Android Beam, Bluetooth, and some others.
I don't know if its an issue with lollipop that broke this functionality or not, as one of my managers sent me code that worked fine for him a few years ago.
My code is:
private void openEmailClient(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{getResources().getString(R.string.contact_feedback_email_address)});
intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.contact_feedback_email_subject_android));
try{
startActivity(Intent.createChooser(intent,intentEmailString));
} catch(android.content.ActivityNotFoundException ex){
Log.e(EMAIL_FAIL_TAG, EMAIL_FAIL);
ex.printStackTrace();
}
}
when you will change your intent.setType like below you will get
intent.setType("text/plain");
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.
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
EDIT
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:someone#example.com"));
intent.putExtra("subject", "my subject");
intent.putExtra("body", "my message");
startActivity(intent);
its working ...
So I solved it. Not ideally but it works better than anything else I have tried.
I followed the google docs on doing it, which says to do this:
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);
}
}
and it now works.
This just finds a default app for handling mail. I'm not sure how it decides, but in my case it opened GMail. On a device without GMail installed, such as the Galaxy S5, it opened their mail client and prompted the user to set up email.
Doesn't give choice of app but it works
Try like this it working fine for me...
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:abc#gmail.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Test App");
intent.putExtra(Intent.EXTRA_TEXT, "Email Body");
startActivity(intent);
Note: it only work if you have email address.
For more information please refer this link Android - Is there a foolproof way to only show possible EMAIL clients?

No application can perform this action, when send email

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, text.getText());
sendIntent.setType("message/rfc822");
startActivity(Intent.createChooser(sendIntent, "Send email with"));
this was my code.
I tried the send mail in emulator. But it shows the no application can perform this action. if anyone knows means tell me
Thanks in advance
You need to use text/plain
intent.setType("text/plain");
Also, the Intent.ACTION_SEND is made for sharing, you may want to use Intent.ACTION_SENDTO to only get the list of e-mail clients, or avoid sharing applications such as Facebook, Twitter, etc.
This worked for me
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail#yahoo.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
There's a better approach if you want to send mail: use Action.SEND_TO:
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:sample#mail.com"));
sendIntent.putExtra(Intent.EXTRA_TEXT, text.getText());
That will narrow down the searchlist.
NOTE: Make sure you have set up email account on emulator, else the Email application will not be in the handlers-list, and you'll get exception.
This means that there is no application that is registered for handling such kind of intent.
Edit:
Try setting the intent type to "text/plain"
emailIntent.setType("text/plain");
and/or set EXTRA_EMAIL to set the content of the email
sendIntent.putExtra(Intent.EXTRA_EMAIL, text.getText());
After trying everything as mentioned in the above comments, if you don't find your solution, Just set an email in the default email in Emulator. It works for me.
private fun sendMail(recipient: String, subject: String, message: String)
{
val mIntent = Intent(Intent.ACTION_SEND)
mIntent.data = Uri.parse("mailto:sample#gmail.com")
mIntent.type = "text/plain"
mIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
mIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
mIntent.putExtra(Intent.EXTRA_TEXT, message)
try {
startActivity(Intent.createChooser(mIntent, "Choose email client"))
Toast.makeText(this, "Hey !! We received your order :) ", Toast.LENGTH_LONG).show()
}catch (e:Exception){
e.printStackTrace()
Toast.makeText(this, "Sorry your order could not be sent :( ", Toast.LENGTH_LONG).show()
}
}
This code works for me, I was facing the same problem,
changing : "val mIntent = Intent(Intent.ACTION_SENDTO)" to
"val mIntent = Intent(Intent.ACTION_SEND)" solved the problem for me.
Hope my response would help the readers
This generally means that there is no application installed currently, that understands the request you're making.
In this instance I would hazard a guess that there is no email app installed on the emulator? Or possibly that it hasn't been set up.

Email intent always launch gmail by default

String body="message";
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Check out this book I am reading");
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
No matter what I do (removing all gmail accounts and signin a hotmail account with mail app), this code launches Gmail by default and do not show or let me choose my universal mail app.
Consequently, there is no way to let user send email via hotmail or other mail provider.
update:
Actually this is the best piece of code I ever come across, it presents you directly with an app chooser where only mail client are present. The answer below will give you a huge list of apps to choose from that are irrelevant.
String mailTo="";
Intent email_intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",mailTo, null));
email_intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here");
email_intent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here");
startActivity(Intent.createChooser(email_intent, "Send email..."));
Try using the correct MIME type (text/plain) instead of an invalid MIME type (plain/text).

How to send an email in android 2.2?

I want to send an email with android 2.2. First I made an intent chooser with an ACTION_SEND to select which to use :
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, Resources.getString("EmailInvitationSubject", getBaseContext()));
String body = Resources.getString("EmailInvitationBody", getBaseContext()) + Local.User.FirstName;
intent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(intent, "Invite friends"));
But in that case, the selector show 'Bluetooth, Messaging, Google+, Gmail'. I want to show ONLY Gmail or other email app.
I saw in the sdk docs there's a new CATEGORY_APP_EMAIL to use but it's only available in the API level 15. I have to keep API level 8. Is there a way to do that ?
By the way, I'll want to do it for messaging too so that in the end I can have 2 buttons: one for email and one for messaging.
This code will shows only the email clients,
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail#yahoo.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
You might want to checkout following (to get what you are looking at the end, i.e. "....By the way, I'll want to do it for messaging too so that in the end I can have 2 buttons: "):
http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android
or also you could checkout:
Android email chooser
Kind regards,
Bo

How does this app access GMail?

I'm writing an Android app that needs to access GMail, and I'd like to do it the same way the SwiftKey does, by showing the user a (Google-hosted?) prompt to login to their Google account, like this:
Do you know what API they're using to get this prompt? Does this API provide direct access to GMail or do I still have to use IMAP?
Thanks in advance...
It seems like oauth being handled via webview which is constructed to look like dialog box.
You need to use Intent for your requirement. Following is the code for same.
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{ "target#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject");
i.putExtra(Intent.EXTRA_TEXT , "body part");
try
{
startActivity(Intent.createChooser(i, "Sending Email..."));
}
catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(MyActivity.this, "No Email clients",Toast.LENGTH_SHORT ).show();
}
This code will allow you to communicate via Gmail Server.

Categories

Resources