Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending html mail if app allows

I want to send a html mail from my application. I know that not all mail clients allow html tags. But I found the constant Intent.EXTRA_HTML_TEXT in the API (http://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT).

My code looks like this, but it shows always just the text and not the html formatted text whatever mail client I use:

 Intent intent = new Intent(Intent.ACTION_SEND);
 intent.putExtra(Intent.EXTRA_SUBJECT, subject);
 intent.putExtra(Intent.EXTRA_TEXT, "Hello World");
 intent.putExtra(Intent.EXTRA_HTML_TEXT, "<html><body><h1>Hello World</h1></body><html>");

 intent.setType("text/html"); // intent.setType("plain/text");

 startActivity(Intent.createChooser(intent,  "Choose Email Client:"));

So where is the mistake?

like image 420
owe Avatar asked Apr 04 '13 15:04

owe


1 Answers

Sorry, not a positive answer because it doesn't seem to work, at least not in a way that's really universal and reliable. Some mailers are happy with this:

String body = "<html>something</html>";
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));

Note that you don't need the new EXTRA_HTML_TEXT, it works with the older one as well. If this covers all you need then you might be OK. But if you also want to address many other possible intent receivers like Facebook, Skype or even apps like Drive or Keep, unfortunately, I couldn't find a perfect solution but I'd very much like to be proven wrong.

Basically, we have three different formats:

String body = "<html>something</html>";
Spanned html = Html.fromHtml(body);
String stripped = html.toString();

and two possible recipients:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
  intent.putExtra(Intent.EXTRA_HTML_TEXT, ???);
intent.putExtra(Intent.EXTRA_TEXT, ???);

I tried all possible combinations but in any of those, there will be some well known and widely used app that doesn't want to play nicely. Either we get HTML tags embedded, or no formatting, or even no text at all...

like image 155
Gábor Avatar answered Oct 27 '22 01:10

Gábor