3

I am trying to send an HTML email from an Android app using an intent. I am using the following code:

            Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",senderMail, null));
            i.setType("text/html");
            i.putExtra(Intent.EXTRA_EMAIL, new String[]{senderMail});
            if (strShare == "Y")
                i.putExtra(Intent.EXTRA_CC, new String[]{email});
            else
                i.putExtra(Intent.EXTRA_BCC, new String[]{email});
            i.putExtra(Intent.EXTRA_SUBJECT, subjectMessage);
            String htmlData = (new StringBuilder()
                    .append("<html><head></head><body><p><b>Hello World!</b></p>")
                    .append("<a>http://www.google.com</a>")
                    .append("<small><p>More content</p></small></body></html>")
                    .toString());
            i.putExtra(Intent.EXTRA_TEXT,fromHtml(htmlData));
            i.putExtra(Intent.EXTRA_HTML_TEXT,htmlData);
            i.setData(Uri.parse("mailto:" + senderMail));
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Utils.displayAlert("There are no email clients installed.", getActivity());
            }

    public static Spanned fromHtml(String source) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY);
            } else {
        return Html.fromHtml(source);
        }
    }

I have also tried using:

            i.setType("message/rfc822");

and I have tried without the EXTRA_HTML_TEXT.

When I select the gmail app to send the email everything works as expected and an HTML email is sent. But when I choose the inbuilt email app the HTML code is rendered correctly in the email composer (so I know it handles HTML code), but when the email arrives at its destination it is a plain text email and the content is plain text with all HTML tags stripped from it.

I am using Android Studio 2.3 and have run the code on two different emulators (Nexus 5 API 23 and Nexus 5 API 21 x86) and on a Motorola phone, all with the same result.

Can anyone tell me what I am doing wrong please?

panda
  • 821
  • 1
  • 9
  • 20
  • http://blog.iangclifton.com/2010/05/17/sending-html-email-with-android-intent/ – bhaskar kurzekar Apr 10 '17 at 10:52
  • http://stackoverflow.com/questions/2544141/send-html-mail-using-android-intent – Nero Apr 10 '17 at 10:54
  • 1
    There are ~2 billion Android devices, spread across thousands of device models. Those devices will have dozens, if not hundreds, of "inbuilt email app" implementations. None of them have to support the sending of HTML-formatted emails. – CommonsWare Apr 10 '17 at 11:23
  • I found this article: http://www.nowherenearithaca.com/2011/10/some-notes-for-sending-html-email-in.html which tells me how limited HTML email is on Android and that basically I can't do what I want to do, which is to include tables and images ultimately. It works fine in IOS. – panda Apr 13 '17 at 01:54

0 Answers0