天天看点

java怎么设置粗体和斜体,使Java中的电子邮件正文文本变为粗体,斜体和更大

java怎么设置粗体和斜体,使Java中的电子邮件正文文本变为粗体,斜体和更大

I send an E-mail with data retrieved throughout my application like such:

String body = formatEmailBody();

Intent i = new Intent(Intent.ACTION_SEND);

i.setType("message/rfc822");

i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); // Set receipient here

i.putExtra(Intent.EXTRA_SUBJECT, "Subject");

i.putExtra(Intent.EXTRA_TEXT , body);

try {

startActivity(Intent.createChooser(i, "Send mail..."));

} catch (android.content.ActivityNotFoundException ex) {

Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();

}

Where formatEmailBody simply creates a String to be send as body of my email.

But a simple string is boring, how may I add bold / italic / bigger text?

I have tried the following to no success:

Html.fromHtml(new StringBuilder()

.append("

Some Content

")

.append("

More content

")

.toString()));

解决方案

Have you taken a look into Intent, especially the section "Constants", where you see :

EXTRA_HTML_TEXT :

A constant String that is associated with the Intent, used with ACTION_SEND to supply an alternative to EXTRA_TEXT as HTML formatted text.

But please note here :

A constant String that is associated with the Intent, used with ACTION_SEND to supply an alternative to EXTRA_TEXT as HTML formatted text. Note that you must also supply EXTRA_TEXT.

So something like the below should improve the odds of the html content being displayed correctly :-

i.setType("text/html");

i.putExtra(Intent.EXTRA_HTML_TEXT, htmlText);

i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(htmlText));