Sending HTML Email With Android Intent

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.

About Ian G. Clifton

Ian currently works as the Director of User Experience for A.R.O. in Seattle, WA where he also leads Android development. Previously, he has worked as an Android developer, web developer, and even a Satellite, Wideband, and Telemetry Systems Journeyman in the United States Air Force. He created the Essentials of Android Application Development LiveLessons video series and wrote Android User Interface Design: Turning Ideas and Sketches into Beautifully Designed Apps. He spends his off time on photography, drawing, developing, and doing technical review for other Android developers. You can follow his posts on this blog or his ramblings on Twitter.
This entry was posted in Code Samples and tagged , , . Bookmark the permalink.

58 Responses to Sending HTML Email With Android Intent

  1. level32 says:

    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.

  2. I would have guessed startActivityForResult also, but if that doesn’t work, I’m not sure what else to try. Sorry!

  3. Jum says:

    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)

    • Jum says:

      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.

      • Revanth says:

        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.

      • Archana says:

        Hi Jum… Can you please give a sample code/example of body with textutil and html.fromhtml that works for you? Thanks in advance.

        • Jum says:

          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:”));

          • Jum says:

            My html code has been removed from my previous comment.
            Just make sure that the variable ‘body’ is a string with html in it.

          • Archana says:

            Thanks for the quick reply Jum…. I tried it in my emulator with k-9 mail client… its nt working.

  4. Marco da Gualdo says:

    Excellent post, thank you very much!

  5. Revanth says:

    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.

  6. Archana says:

    Thanks for the quick reply Jum…. I tried it in my emulator with k-9 mail client… its nt working.

  7. nia says:

    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

    • nia says:

      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.

      • nia says:

        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!!

        • nia says:

          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

    • nia says:

      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.

  8. NBS says:

    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.

  9. Pingback: Mail Attachment - Seite 2 - Android-Hilfe.de

  10. Björn L says:

    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?

  11. mudit says:

    It is working fine on 2.2 also, no html tags in gmail client. Thanks dude…

  12. Björn L says:

    Got it working now. Tested it on a Android 1.6 device and it seem to work as well. Thanks!

  13. peterlawn says:

    it works fine!

    but if i embed a , the picture will not appear , what’s wrong??

  14. Pads says:

    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 :

  15. Pads says:

    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.

  16. PM says:

    Excellent post, found very much useful in my application, thanx for saving my valuable time

  17. Ashok Reddy says:

    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

  18. Ashok Reddy says:

    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

  19. Ashok Reddy says:

    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.

      • hardy says:

        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

  20. kalyan says:

    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…..

    • narsath says:

      in the manifest file i placed “android:noHistory=”true”” that is the problem ..if i deleted that pat of code ..it is working fine :)

  21. kalyan says:

    in the manifest file i placed “android:noHistory=”true”” that is the problem ..if i deleted that pat of code ..it is working fine :)

  22. Praveena says:

    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

    • Gaurav Mehta says:

      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 !!!!!!!!!

  23. Rory says:

    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

  24. finally found it. great!

  25. Regis says:

    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 ! !

  26. Bill Lahti says:

    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.

  27. Devin says:

    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”

  28. Dattatraya says:

    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.

  29. Janice says:

    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.

  30. Kislay says:

    Can anyone tell me how can i define tags which are not being defined by android. I have tried using div, font n many more tags which are not being understood by Html.fromHtml(data).

  31. Sudarshan says:

    There is a limited number of HTML tags that this supports. Not all the tags are supported :)

  32. Hi there, was looking for how to do this for a while, but I kind of have half a problem. My paragraphs have space after them so I’m assuming it’s working. My LI items are all merged together into one line.

    I am specifically setting the gmail app in the launch so I’m a bit confused. I have also tried all the ordering stuff without any change. Is this something you’re aware of?

    Thanks
    Nigel

  33. Garychen says:

    can anyone tell me i want get the following in my Gmail email
    sno item quantity price total
    1 soap 4 30 120
    2 fan 1 450 450

  34. Helen says:

    Which html tags can be sent via html email? I always get plain text instead. Does it just allow bold, underline, italic, and NOT 100 other tags? Not even simple TABLE tags? (I use gmail as my default email.)

  35. malu says:

    Hi,

    I tried your second code,it is working fine for me but in my mail the html tags also coming.In my case i am taking from my server.The log cat out put is:

    03-20 15:56:34.210: E/WebView Activity(10953):
    03-20 15:56:34.210: E/WebView Activity(10953):
    03-20 15:56:34.210: E/WebView Activity(10953):
    03-20 15:56:34.210: E/WebView Activity(10953): body
    03-20 15:56:34.210: E/WebView Activity(10953): {
    03-20 15:56:34.210: E/WebView Activity(10953): color: Black;
    03-20 15:56:34.210: E/WebView Activity(10953): font-size: small;
    03-20 15:56:34.210: E/WebView Activity(10953): font-family: Arial;
    03-20 15:56:34.210: E/WebView Activity(10953): }
    03-20 15:56:34.210: E/WebView Activity(10953): H1
    03-20 15:56:34.210: E/WebView Activity(10953): {
    03-20 15:56:34.210: E/WebView Activity(10953): font-size: medium;
    03-20 15:56:34.210: E/WebView Activity(10953): text-align: left;
    03-20 15:56:34.210: E/WebView Activity(10953): padding: 5;
    03-20 15:56:34.210: E/WebView Activity(10953): }
    03-20 15:56:34.210: E/WebView Activity(10953): H3
    03-20 15:56:34.210: E/WebView Activity(10953): {
    03-20 15:56:34.210: E/WebView Activity(10953): color: #59160c;
    03-20 15:56:34.210: E/WebView Activity(10953): padding: 0 10 0 10;
    03-20 15:56:34.210: E/WebView Activity(10953): font-size: small;
    03-20 15:56:34.210: E/WebView Activity(10953): }

    i need to discard this html contents from my mail.please help me

Comments are closed.