Send HTML e-mail with Android formatting (style inline)

Asked

Viewed 710 times

3

I am trying to send a personalized email through new Intent(Intent.ACTION_SEND), containing in the email body a formatted HTML, but when selecting the Gmail application, for example, the whole style html is ignored. What I’m trying to do is the following:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/html");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<h1>Sou um H1</h1><p>Eu sou um paragrafo</p><p style=\"color:red;background-color:black;\">Eu sou um paragrafo colorido!</p>"));
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(getActivity(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

The email application is called correctly, but the part of the style of HTML is lost. It is possible to keep HTML intact in this way to send email through Intent.ACTION_SEND?

1 answer

1

Just use the Intent.EXTRA_HTML_TEXT parameter

i.sendIntent.setType(“text/html”);

i.putExtra(Intent.EXTRA_HTML_TEXT, Html.fromHtml("<h1>Sou um H1</h1><p>Eu sou um paragrafo</p><p style=\"color:red;background-color:black;\">Eu sou um paragrafo colorido!</p>").toString());

OR

i.putExtra(Intent.EXTRA_HTML_TEXT, "<h1>Sou um H1</h1><p>Eu sou um paragrafo</p><p style=\"color:red;background-color:black;\">Eu sou um paragrafo colorido!</p>");
  • Did that work for you? Because I tested it here and it didn’t work! If only the Intent.EXTRA_HTML_TEXT as you suggested, give the following alert on the console: Key android.intent.extra.HTML_TEXT expected String but value was a android.text.SpannableStringBuilder. The default value <null> was returned. and nothing appears in the body of the email. If Intent.EXTRA_TEXT it appears only the contents of Intent.EXTRA_TEXT in the body of the e-mail and not the Intent.EXTRA_HTML_TEXT. I’m testing on Android 5.0, and selecting the email manager of Gmail.

  • Sorry, there are two ways. I will change the possibilities, if it doesn’t work with you, I remove the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.