如何发送HTML电子邮件电子邮件、HTML

2023-09-11 12:20:06 作者:惯性失恋

我发现了一种使用意图发送纯文本电子邮件:

i found a way to send plain text email using intent:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain"); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new     
String[]{"example@mail.com"}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Test");

但我需要发送HTML格式的文本。 尝试的setType(text / html的)不工作。

But I need to send HTML formatted text. Trying to setType("text/html") doesn't work.

推荐答案

您可以通过跨区在额外的文本。为确保意图解析才活动处理电子邮件(如Gmail和电子邮件应用程序),你可以使用 ACTION_SENDTO 用开放的开头mailto方案。这也将工作,如果你不知道收件人事先:

You can pass Spanned text in your extra. To ensure that the intent resolves only to activities that handle email (e.g. Gmail and Email apps), you can use ACTION_SENDTO with a Uri beginning with the mailto scheme. This will also work if you don't know the recipient beforehand:

final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject");
shareIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
    .append("<p><b>Some Content</b></p>")
    .append("<small><p>More content</p></small>")
    .toString())
);