How to send HTML email

2019-01-02 20:47发布

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");

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

6条回答
低头抚发
2楼-- · 2019-01-02 21:30

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())
);
查看更多
墨雨无痕
3楼-- · 2019-01-02 21:33

What about just trying to add some html in the text area?

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "<strong>Test</strong>");
查看更多
高级女魔头
4楼-- · 2019-01-02 21:40

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.

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.

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)

查看更多
伤终究还是伤i
5楼-- · 2019-01-02 21:42

I haven't (yet) started Android development, but the documentation for 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...

查看更多
君临天下
6楼-- · 2019-01-02 21:44

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:

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="yourmail@gmail.com";
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..."));
查看更多
看淡一切
7楼-- · 2019-01-02 21:48
登录 后发表回答