Android 如何发送 HTML 电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2007540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send HTML email
提问by Denis Palnitsky
i found a way to send plain text email using intent:
我找到了一种使用 Intent 发送纯文本电子邮件的方法:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new
String[]{"[email protected]"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Test");
But I need to send HTML formatted text.
Trying to setType("text/html") doesn't work.
但我需要发送 HTML 格式的文本。
尝试 setType("text/html") 不起作用。
回答by antnerves
You can pass Spannedtext 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_SENDTOwith a Uri beginning with the mailto scheme. This will also work if you don't know the recipient beforehand:
您可以Spanned在 extra 中传递文本。为确保意图仅解析处理电子邮件的活动(例如 Gmail 和电子邮件应用程序),您可以使用ACTION_SENDTO以 mailto 方案开头的 Uri。如果您事先不知道收件人,这也将起作用:
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())
);
回答by Andy Weinstein
This was very helpful to me for the HTML, but the ACTION_SENDTO didn't quite work for me as is - I got an "action not supported" message. I found a variant here which does:
这对我的 HTML 非常有帮助,但是 ACTION_SENDTO 对我来说并不完全适用 - 我收到了“不支持操作”的消息。我在这里找到了一个变体:
http://www.coderanch.com/t/520651/Android/Mobile/no-application-perform-action-when
http://www.coderanch.com/t/520651/Android/Mobile/no-application-perform-action-when
And here's my code which combines the two together:
这是我将两者结合在一起的代码:
String mailId="[email protected]";
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.fromParts("mailto",mailId, null));
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here");
// you can use simple text like this
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here");
// or get fancy with HTML like this
emailIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<p><b>Some Content</b></p>")
.append("<a>http://www.google.com</a>")
.append("<small><p>More content</p></small>")
.toString())
);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
回答by Chris Shaffer
I haven't (yet) started Android development, but the documentationfor the intent says that if you use EXTRA_TEXT, the MIME type should be text/plain. Seems like if you want to see HTML, you'd have to use EXTRA_STREAM instead...
我还没有(还)开始 Android 开发,但是Intent的文档说如果你使用 EXTRA_TEXT,MIME 类型应该是 text/plain。似乎如果您想查看 HTML,则必须改用 EXTRA_STREAM...
回答by SMGhost
Been trying to send html via gmail app for a while, so decided to leave some insight on what I found, just in case someone else is having similar issues.
尝试通过 gmail 应用发送 html 有一段时间了,所以决定对我的发现留下一些见解,以防其他人遇到类似的问题。
Seems like no matter what I did, I couldn't get the html to have bold text in it. Then I've tried switching to outlook client and to my surprise it was working just fine. Html markup was also working on other older devices, but not on mine (galaxy s7 API 26), so I figured, that gmail app seems to have dropped support for html syntax that comes from intent or maybe now you're required to provide it in some very specific way which is not clearly documented.
似乎无论我做什么,我都无法让 html 中包含粗体文本。然后我尝试切换到 Outlook 客户端,令我惊讶的是它工作得很好。Html 标记也适用于其他较旧的设备,但不适用于我的(galaxy s7 API 26),所以我想,那个 gmail 应用程序似乎已经放弃了对来自意图的 html 语法的支持,或者现在您需要提供它以某种没有明确记录的非常具体的方式。
Last gmail version that worked for me was version 6.9.25... on Nexus 5X API 25 emulator (Nougat) And it stopped working starting version 7.5.21... On Nexus 5x API 26 emulator (Oreo)
对我有用的最后一个 gmail 版本是 6.9.25 版......在 Nexus 5X API 25 模拟器(牛轧糖)上它停止工作从 7.5.21 版开始......在 Nexus 5x API 26 模拟器(奥利奥)上
回答by Rodrigo Bermejo
You must to change "EXTRA_TEXT" for "EXTRA_HTML_TEXT"
您必须将“EXTRA_TEXT”更改为“EXTRA_HTML_TEXT”
https://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT
https://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT
回答by EpicDewd
What about just trying to add some html in the text area?
只是尝试在文本区域中添加一些 html 怎么样?
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "<strong>Test</strong>");

