Sending HTML email from Android app using an intent
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?
链接地址: http://www.djcxy.com/p/23266.html