It’s very easy to send email via an Android intent. Here’s an example where we already have the subject and body prepared but want to let the user decide on the recipient:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Email:"));
(It’s important to note that this should be attempted on a real device.)
I ran into some trouble with sending HTML in an email because it was being interpreted as plain text both in the user’s email client and in the recipient’s. Simply changing the MIME type didn’t help, but I eventually came across the solution:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
startActivity(Intent.createChooser(emailIntent, "Email:"));
Note that both the MIME type is changed and the EXTRA_TEXT is now set as Html.fromHtml(body) instead of just being passed a string with HTML in it. As long as the mail app that is selected is able to properly handle the Intent (e.g., the Gmail app can, the default mail app cannot), then HTML email can be sent.
Nice tip. Do you think there’s a way to see if message was sent? I tried to use startActivityForResult, but onActivityResult never got called.
I would have guessed startActivityForResult also, but if that doesn’t work, I’m not sure what else to try. Sorry!
I have just try your second code to send html message and i have a little problem.
When I chose the gmail app to send the email, all the html code is put in the body of the message in plain text.
Do you have this problem too ?
(my variabel ‘body’ is a string with HTML in it)
It’s working !
I was doing a TextUtils.htmlEncode(body) befaure calling Html.fromHtml.
Just remove the htmlEncode and it’s solve the problem.
Thank you very much.
Hi,
I tried your second code to send html message and its working fine for gmail but its not working for HTC email.where in nothing gets displayed in body of the mail.I tried it in HTC Hero.
Hi Jum… Can you please give a sample code/example of body with textutil and html.fromhtml that works for you? Thanks in advance.
Actually my working code is exactly the seconde exemple of Ian.
I don’t use TextUtils, it was a mistake.
Here is a working code :
StringBuilder sb = new StringBuilder();
sb.append(“”)
.append(“”)
.append(“”)
.append(“”)
.append(“”)
.append(“”)
.append(“Test”);
.append(“”);
body = sb.toString();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType(“text/html”);
sendIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, “subject”);
startActivity(Intent.createChooser(sendIntent,”Email:”));
My html code has been removed from my previous comment.
Just make sure that the variable ‘body’ is a string with html in it.
Thanks for the quick reply Jum…. I tried it in my emulator with k-9 mail client… its nt working.
Excellent post, thank you very much!
Hi,
I tried your second code to send html message and its working fine for gmail but its not working for HTC email.where in nothing gets displayed in body of the mail.I tried it in HTC Hero.
It could be that the HTC email client that is included in the Hero does not support HTML email. Unfortunately, I don’t have a Hero to test with.
Thanks for the quick reply Jum…. I tried it in my emulator with k-9 mail client… its nt working.
hi thanks fr the amazing tut..
i m facing a problem..the link which i want to send comes as hyperlink while sending mail..ie i c it dt its a hyperlink
BUT on recieving mail it gets converted back to text..any idea what might be happening??r i might b doing wrng
it would b really great if u could help
thanks in advance
i m trying to send mail through gmail
I would take a look at your sent box to see if it is still HTML there. Otherwise it may be an issue with the receiving email box only viewing the plain text version.
thank u fr such a quick reply..
it possibly cannot be a case f receiving email in text version becoz…
the mail i m sending has a share link which is obtained by WebView.getUrl() ..
which is seen as text during sending mail..but becomes hyperlink on being recieved
n i want to send another link in d mail so i make it a hyperlink using
link ..dis link is shown as hyperlink whn i m sending ..but converts to text on being recieved
totally confused!!
hmm..let me b more precise
String val=text+ “ click here“;
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,title);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(val));
emailIntent.setType(“text/html”);
startActivity(Intent.createChooser(emailIntent, “Send mail…”));
what m i missing??pls help
I’m not sure if the order is significant, but my first guess would be to put the setType call before adding the extras.
hi thanks fr the reply,
i hv already tried d code with setType bfore extras becoz dt ws d only diffrence in our codes i could point out…no effect..still d same i c link i want to send as hyperlink while composing mail..whch changes back to text on being sent
also frm wht i hv searched on net i guess gmail sets mime type to default plain/text while sending mssg…due to which only text is recived..loads f ppl hv faced dis problem…nt sure if i m crrct on dis
1)http://code.google.com/p/android/issues/detail?id=3951
2)http://forums.androidandme.com/topic/sending-email-with-html-in-body
also i tried using ur app cbs news..share app function wrked perfectly in it..u hv used d same above code fr dt..r some change..
pls help!
That’s strange that the CBS News app would work correctly but your code didn’t. My next guess would have been something with the configuration of the email client, but the code used in it is the same with the one exception of putting the startActivity(emailIntent) in a try/catch, which shouldn’t affect the email itself. My only other thought would be to verify your version of Java (the computer I’m on now has 1.6.0_18), ensure your Android SDK is up to date, and possibly try other build targets. It might be easiest to just create a new project with a simple activity that triggers the above code in its onCreate method to see if that works. If it does, then start making changes to it one at a time to bring it closer to what you are trying to use now that isn’t working. If it doesn’t work, then it must be a factor outside your code.
Hi,
I try to use html formated text mail.
I try with the second code and it works fine.
But I have one problem. I want to send the html with hyperlink.
I try with code like
Example
But when I send the mail, it come only Example not the hyperlink.
What can I do to send with hyperlink.
Pingback: Mail Attachment - Seite 2 - Android-Hilfe.de
Have anyone actually got an <a href..-link working in a html mail sent or is it always sent as plain text with complete url that href points to, printed out?
It is working fine on 2.2 also, no html tags in gmail client. Thanks dude…
Got it working now. Tested it on a Android 1.6 device and it seem to work as well. Thanks!
it works fine!
but if i embed a , the picture will not appear , what’s wrong??
correct :
if i embed a , the image will not appear , what’s wrong??
Hi
I have tried ur second set of code :
String title = “PICTURE”;
String link = some link;
String data = “Hello, \n I want you to see this \n \n” + Html.fromHtml(““+title+”) + “\n” + link;
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType(“text/html”);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, data);
context.startActivity(Intent.createChooser(emailIntent, “Email:”));
in this, string data contains html code. for example :
Sorry clicked submit before completing.
But its not working for me. I have tried with HTC hero through gmail and inbuilt mail. Anyways its not working.
Excellent post, found very much useful in my application, thanx for saving my valuable time
Even if i use the following code it didnot work for me to send the mail in html format.
can anybody suggest me any other way……
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType(“text/html”);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, “subject”);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,data);
startActivity(emailIntent);
data contains the html tags….
Thanks in Advance
Even if i use the following code it didnot work for me to send the mail in html format.
can anybody suggest me any other way……
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType(“text/html”);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, “subject”);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,data);
startActivity(emailIntent);
data contains the html tags….
Thanks in Advance
sry…..i used Html.fromHtml(data); in place of data
even it didnot work…
do i need to change the manifest file……?
Sending the HTML email requires that the mail app that is selected is able to properly handle the passed String. From what I’ve seen, the Gmail app works but the default Mail app does not.
Ian,
I second Ian’s observation. I can send html mail with links using gmail, but not mail app. Also, mail can receive the html correctly, but gmail cannot. So I’m stuck having to send mail out using gmail and read it using builtin mail to have basic html mail features work.
- Hardy
i tried this code in my application ..but after sending the mail if i click on back button the application is closed..it will not going to my previous page ….what i have to do..how can i do this?can anyone help me to solve this ..thanks in advance…..
in the manifest file i placed “android:noHistory=”true”" that is the problem ..if i deleted that pat of code ..it is working fine
in the manifest file i placed “android:noHistory=”true”" that is the problem ..if i deleted that pat of code ..it is working fine
i want to send the message with bullets .I already tried with tag of html.but it is not working.Give me some code suggestions.Thanks in advance
hi Praveena,
I am facing the same problem which you were discussing above. I want to show a formated text in email intent which contain bullets and tables Please help me out i am searching the solution for last 5 days !!!!!!!!!
Hello
I am attempting to send HTML emails. I have tried many permutations, however I end up with plane text in the Gmail application. I get the following in my Gmail email: “TestHeaderTesting HtmlUsed Gmail”
The following code seems to be exactly as recommended from the above posts:
final StringBuilder sb = new StringBuilder();
sb.append(“Test”);
sb.append(“”);
sb.append(“”);
sb.append(“”);
sb.append(“Header”);
sb.append(“”);
sb.append(“”);
sb.append(“Testing Html”);
sb.append(“”);
sb.append(“”);
sb.append(“Used Gmail”);
sb.append(“”);
sb.append(“”);
String strHtml;
strHtml = sb.toString();
Intent email_intent2 = new Intent(Intent.ACTION_SEND);
email_intent2.setType(“text/html”);
email_intent2.putExtra(Intent.EXTRA_SUBJECT, “Testing HTML”);
email_intent2.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(strHtml));
startActivity(email_intent2);
Thank you for your help
finally found it. great!
Would it be possible to update this excellent tuto to explain how to do in order to do so that the html links are also displayed in the standard Android email client ? (not only in Gmail) Send me an email if you do that. Thanks ! !
Thanks a lot for taking the time to write this up. I had used that Html method for text views and had not thought of using it for email too.
I am trying to get an android app to send an email but my emulator quits working when it gets to the startActivity() in my code. The code follows:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, “dckeep1@gmail.com”);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, “Benford’s Law Results”);
emailIntent.setType(“plain/text”);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, output.getText().toString());
startActivity(emailIntent);
Do you have any suggestions as to why?
I’d guess that it’s not finding an Activity that matches your Intent. Check the log output to see if that’s the error (and if it is, it should show what Intent it is trying to match). You can also add the FLAG_DEBUG_LOG_RESOLUTION flag to your Intent to get more information in your logs.
Also, your type should be “text/plain” rather than “plain/text”
Thanks, It is really a good tutorial. I have one query, when i am calling the emailIntent it go to the send mail screen, but if i click on cancel button(on send mail screen) the it destroy this screen and come back to previous screen. In this case i want to get the notification whether i send the mail or cancel to send mail or move the mail in draft.
Is there any way to get this notification.
Unfortunately, I don’t know of any way to accomplish that using the default mail client.
Can someone post a screen shot of the open email form… with the html code in it… right before you hit SEND.
I’ve tried a lot of code… including the code on this site. Never sends any html… and the html doesn’t even make it into the mail form.