I'm trying to send an Email via an Android App. Currently using Android Studio 1.2 and testing on Genymotion 2.4.0 on a Galaxy Nexus 4.3(API 18) image with ARM Translation and Google apps installed. I know this question is very similar to this, but I've tried every suggestion there to no effect.
I can retrieve the email(where to send) and the "body" of the email, but unnable to get the email Subject. Code is as follows:
Intent iEmail = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "email#somewhere.com", null));
iEmail.setType("text/plain");
// Both these "options" don't work
iEmail.putExtra(Intent.EXTRA_SUBJECT, etTema.getText());
iEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
// This works fine
iEmail.putExtra(Intent.EXTRA_TEXT, etDesc.getText());
startActivity(Intent.createChooser(iEmail, "Choose:"));
When the Activity starts and I choose the email client email(to send to) is displayed correctly as is the "body" but the subject remains blank.
EDIT: result is always the same whether i use the EditText(etTema) or try passing a string.
Any suggestions? Thanks
Your code should work, but I remember using:
String uriText = "mailto:youremail#gmail.com" +
"?subject=" + Uri.encode("some subject text here") +
"&body=" + Uri.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email"));
See if that works better across devices / APIs for you.
Try just doing this. This works great for me.
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "email#somewhere.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, etDesc.getText());
startActivity(Intent.createChooser(emailIntent, "Send Email"));
Related
This question already has answers here:
Send HTML mail using Android intent
(5 answers)
Closed 3 months ago.
I want to send email through my application. I need to send HTML based email just through G-Mail. I found following solutions that each of them has pros and cons.
1) Using Intent (Intent.ACTION_SEND). This is very simple way and I can see my body in HTML format but the problem is when I click on "Send email" button, so many applications like Facebook and Google+ pop up which are useless and I shouldn't show it in that list. This is its code:
String html = "<!DOCTYPE html><html><body>Visit W3Schools.com!" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"MY EMAIL ADDRESS"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(html));
startActivity(Intent.createChooser(intent, "Send email..."));
2) Using Intent (Intent.ACTION_SENDTO). This way Filters useless applications and shows me just mail clients. But it doesn't display my email in HTML format in gmail client. When i send the email some clients show the body in HTML format while others doesn't identify HTML and my link behaves like plain text. This code is like:
String html = "<!DOCTYPE html><html><body>Visit W3Schools.com!" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:MY EMAIL ADDRESS" + "?subject=subject here" + "&body=" + html;
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
3) Sending mail using JavaMail API which adds so much complexity to application and I didn't test it so far.
What is your suggestion? I need a way to have advantages of first and second ways. I need when user click on button it shows Gmail client and I can show him/her html content in body part of client.
any suggestion would be appreciated. Thanks
======================
Update
Something about code 2 is wrong. The code is like this:
String html = "<!DOCTYPE html><html><body>Visit W3Schools.com!" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:MY EMAIL ADDRESS" + "?subject=subject here" + "&body=" + Html.fromHtml(html);
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
Try the following -
Intent shareIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(body));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
startActivity(shareIntent);
This will only present email applications.
If you want only one application to handle your intent then you need to remove Intent.createChooser(), rather jst use startActivity()---> it send the mail using default email client, if not set then will ask to do so... tat can be changed anytime
To get only email applications, use
Intent.setType("message/rfc822")
I'm trying to use this solution but Eclipse still cant resolve "Attachment" and "DataService".
I've imported mail.jar and activation.jar, what could I be doing wrong? I've tried countless other sending email solutions on SO/Google but I couldn't get any of them to work with my attachment and send HTML emails.
Any help would be appreciated.
If you need to construct email to be sent by user manually (open his mail program with new email and attachment) you can use this code:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:" ));
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "SUBJECT");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "MESSAGE");
File toAttach = new File("/path/to/your/file");
Uri uri = Uri.fromFile(toAttach);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(emailIntent);
I used this tutorial and changed
messageBodyPart.setText(_body);
to
messageBodyPart.setContent(_body, "text/html");
I have a problem with setting type "message/rfc822" for intent to send e-mail with file attachment on Android emulator. I have to use setType("message/rfc822") because the file doesn't have standard MIME-type (SQLite database) and I am trying to avoid a lot of applications in the select list for user's choice. For all API Levels before 2.3.3 I have an error:
java.lang.RuntimeException:
Unable to start activity ComponentInfo{my.cashwatcher/my.cashwatcher.SendEmailActivity}:
android.content.ActivityNotFoundException:
No Activity found to handle Intent { act=android.intent.action.SEND typ=message/rfc822
(has extras) }
In the case of API Level, 2.3.3 code works fine and error doesn't appear. Is it a problem with the Android emulator or old APIs!?
Code:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("message/rfc822");
sendIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{appPrefs.getEmail("email")});
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), DATABASE_PATH)));
sendIntent.putExtra(Intent.EXTRA_TEXT, "body_of_email");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "APPLICATION_NAME");
startActivityForResult(sendIntent, EMAIL_SEND_RESULT);
First, "to avoid a lot of applications in select list for user's choice", use ACTION_SENDTO and a mailto: Uri.
Second, what you are experiencing is not "a problem of Android emulator" nor "old APIs". You need 1+ applications that are capable of handling the ACTION_SEND Intent and a MIME type of message/rfc822. There is no guarantee that any given device will support that combination, let alone any given emulator. Your code needs to handle that, just as if you use ACTION_GOBBLEDYGOOK or a MIME type of thisis/sonotreal or whatever.
I have made an application that uses URI example as you desired:
if(v.getId()==R.id.button3)
{
intent=new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto"));
String[]to={"akshkatheria#gmail.com","megakatheria#gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, "hello");
intent.putExtra(Intent.EXTRA_TEXT, "hi");
intent.setType("message/rfc822");
chooser=intent.createChooser(intent, "send mail");
startActivity(chooser);
}
This is the solution. Use the below code, works perfectly...Got the
solution after research.... :)
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);
I am trying to send a pdf as an attachment from Android. Here is the code:
String[] mailto = {"me#gmail.com"};
Uri uri = Uri.parse("android.resource://com.mywebsite.sendemail/raw/mypdf");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "My Body");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email using:"));
Now this works but the problem is that the attachment is called mypdf instead of mypdf.pdf. I cannot figure out how to send it with it's extension... That's what I need help with. Thanks.
I am unconvinced what you want will be possible, since you are pulling the PDF from a resource. If you copy the PDF to a local file (with the correct extension) and send that, you should get the extension in the resulting message. But straight out of the resource...I suspect there's no way to add the extension.
When ever I attempt to use the .putExtra methodology it always crashes my application with a "Force Close" message. If I use something like:
String mtUri = "mailto:someone#gmail.com?subject=Some Subject&body=Some text&";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(mtUri));
startActivity(intent);
It seems to work fine. I do still have the problem of attaching a file and could use some help figurint out the "attachment=file:///..." syntax.
Thanks,
i found a way to send plain text email using intent:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new
String[]{"example#mail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Test");
But I need to send HTML formatted text.
Trying to setType("text/html") doesn't work.
You can pass Spanned text in your extra. To ensure that the intent resolves only to activities that handle email (e.g. Gmail and Email apps), you can use ACTION_SENDTO with a Uri beginning with the mailto scheme. This will also work if you don't know the recipient beforehand:
final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject");
shareIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<p><b>Some Content</b></p>")
.append("<small><p>More content</p></small>")
.toString())
);
Been trying to send html via gmail app for a while, so decided to leave some insight on what I found, just in case someone else is having similar issues.
Seems like no matter what I did, I couldn't get the html to have bold text in it.
Then I've tried switching to outlook client and to my surprise it was working just fine.
Html markup was also working on other older devices, but not on mine (galaxy s7 API 26), so I figured, that gmail app seems to have dropped support for html syntax that comes from intent or maybe now you're required to provide it in some very specific way which is not clearly documented.
Last gmail version that worked for me was version 6.9.25... on Nexus 5X API 25 emulator (Nougat)
And it stopped working starting version 7.5.21... On Nexus 5x API 26 emulator (Oreo)
This was very helpful to me for the HTML, but the ACTION_SENDTO didn't quite work for me as is - I got an "action not supported" message. I found a variant here which does:
http://www.coderanch.com/t/520651/Android/Mobile/no-application-perform-action-when
And here's my code which combines the two together:
String mailId="yourmail#gmail.com";
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.fromParts("mailto",mailId, null));
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here");
// you can use simple text like this
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here");
// or get fancy with HTML like this
emailIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<p><b>Some Content</b></p>")
.append("<a>http://www.google.com</a>")
.append("<small><p>More content</p></small>")
.toString())
);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
I haven't (yet) started Android development, but the documentation for the intent says that if you use EXTRA_TEXT, the MIME type should be text/plain. Seems like if you want to see HTML, you'd have to use EXTRA_STREAM instead...
You must to change "EXTRA_TEXT" for "EXTRA_HTML_TEXT"
https://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT
What about just trying to add some html in the text area?
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "<strong>Test</strong>");