Some Notes for Sending HTML Email in Android

I am in the middle of working on an android app that needs to be able to send a table of records in an email. Ideally, I would be able to send a pretty html table, because that's what the iPhone version of this app can do... and it's apparently very simple to do it "over there".

Using the (obvious) "just start an activity with the right Intent.ACTION_SEND intent" does not work, as android will not pass the <table> tag to whatever intent may want to handle it. This turns out to be an example of one of those things that, although android can get you close to your solution with a few lines of code, you're gonna have to work a bit for it to get anywhere close to what you want.

This led me to explore exactly which html tags you can pass this way that will end up in the email.

In Android, the basic idea in sending html in email is to take some html in a string, then pass it to Html.fromHtml, and stick what you get back into the Intent.EXTRA_TEXT extra for the intent. The fromHtml method ultimately will call Html.HtmlToSpannedConverter.handleStartTag/handleEndTag, and that is what I looked at to derive the table below (Android 2.1-2.3).

Note that you could write your own Html.TagHandler to handle other tags, but this won't matter to the Intent that receives the data because it won't know what to do with the information. I think you'll have to wait on the framework to change. If you really want all that extra html in an email, you'll need to go low-level and use the java mail api directly.

In my case, since the <tt> tag is supported, I can at least get some very basic "tables" whose columns do not get mangled with a gmail client when sent from the gmail android app.

You need to write your own Html.ImageGetter for this to work - I've haven't messed with this yet

Anything else

You can implement a Html.TagHandler to deal with this, but won't matter if data is passed to an external Intent outside of your app

Note - there still be quirks out there - for example, apparently the default email client for 2.2 and earlier couldn't handle html at all (http://code.google.com/p/android/issues/detail?id=16109).
For the time being, the best advice is probably to just avoid html email if you can!

Final note - it is a simple matter to write the html to a file, and then attach the file to the email by using the EXTRA_STREAM extra for the intent. However, besides adding an extra step for the receiving user to view the information, this requires that the file be readable by any process, which might not be acceptable depending on the nature of the data involved.

The purpose of this page is to summarize in one place some of the interactive visualizations I have worked on. Most of these were built...

"When you start on your journey to Ithaca, then pray that the road is long, full of adventure, full of knowledge... Always keep Ithaca fixed in your mind. To arrive there is your ultimate goal. But do not hurry the voyage at all." (from "Ithaca", by C. P. Cavafy)