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.
Related
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.
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!!!
I'm trying to send an HTML text from my app by email. So I have this code:
StringBuilder sb = new StringBuilder();
// Lots of HTML building code...
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, trip_to_manipulate.getTrip_name());
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(sb.toString()));
i.setType("message/rfc822");// to filter and display only email apps
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
But when I run this code I have whole bunch of apps showing up in the chooser:
I tried to start activity without createChoser, I tried to change type to text/html, I tried to use ACTION_SENDTO instead of ACTION_SEND, I tried them separately, I tried them combined, but I still got all these apps. Sometimes even more. And when they were combined the app crashed.
So is there a way to limit this list to email clients only?
ShareActionProvider provides share for every compatible app on the device. I would like to specify which apps I want to allow sharing, like only twitter and gmail for example.
How would I do this?
Google don't recommend that you share to specific apps as there are various different Twitter clients - it's better to just let people chose where they want to share it.
However, the following code will send the message "hello twitter" to twitter on a button press.
String message = "hello Twitter";
try {
//try to open the official twitter app
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(
android.content.Intent.EXTRA_SUBJECT, "Subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, message);
sharingIntent.setPackage("com.twitter.android");
startActivity(sharingIntent);
} catch (Exception e) {
//fallback on opening an internet browser
Log.e("Danielle", "exception=" + e.toString());
Intent i = new Intent();
i.putExtra(Intent.EXTRA_TEXT, message);
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri
.parse("https://mobile.twitter.com/compose/tweet"));
startActivity(i);
}
Hope that helps.
If you are ok with just using a popup, the answer by dacoinminster in this question How to filter specific apps for ACTION_SEND intent (and set a different text for each app) will do it.
But if you specifically want it to be an actionprovider dropdown, I have not find this solution either.
I had shown the HTML file content in WebView.That is available in SDCard, I need to send that HTML Content as a email in the same format(HTML).
Note: I don't want to send it from email client application.I need to send it without user interaction
At this other Answer there is a nice explanation of using javaMail API. and other option is you can use mailchamp library.
Yes, It is possible
1st off, give the manifest permission to
<uses-permission android:name="android.permission.INTERNET" />
then follow the tutorial
but little bit tricky, check this tutorial
http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android
Do this way
String htmalContentFromSdcard = "";
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/html");
i.putExtra(Intent.EXTRA_EMAIL, "abc#gmail.com");
i.putExtra(Intent.EXTRA_SUBJECT, "Demosubject");
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(htmalContentFromSdcard));
try {
startActivity(Intent.createChooser(i, "Send mail..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
ting(getString(R.string.share_email_no_client));
}
Hope this helps you.