如何在 android 中以 html 格式的电子邮件发送图像?

Posted

技术标签:

【中文标题】如何在 android 中以 html 格式的电子邮件发送图像?【英文标题】:How to send image in html formatted email in android? 【发布时间】:2017-10-03 10:48:05 【问题描述】:

我想通过我的应用程序发送 html 格式的电子邮件, 但发送电子邮件时不支持所有标签的样式。

这是我的 Java 代码:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null));
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Look What I Found!");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
      Html.fromHtml(mMessage)
);
startActivity(emailIntent);`

这是我的 html,我要发送的内容:

"<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <center> <table style="width: 600px; margin:0 auto" cellpadding="0" cellspacing="0"> <tr> <td> <table> <tr> <td> <img src="https://www.twinspires.com/sites/twinspires.com/files/header-600x123.jpg" alt="Churchill Downs" style="display: block;width: 600px;height: 123px;">	</td> </tr> </table> <table style="padding:40px 0px;"> <tr> <td style="width: 50px;"> </td> <td> <p style="font-family: 'Open Sans', Helvetica, Arial, sans-serf;text-align: justify; font-style: 14px;">View this shared article from the Churchill Downs Incorporated Magazine $title. Download the official lifestyle app of Churchill Downs – "Club 1875" to access the elegant world of Thoroughbred racing and the Kentucky Derby. Stay up to date on fashion, food, celebrities and some of the most recognizable names in the industry. Club 1875 is your behind the scenes guide to the most exciting two minutes in sports – whether you’re at the track or far away! </p> </td> <td style="width: 50px;"> </td> </tr> </table> <center> <table> <tr> <td> <img src="https://www.twinspires.com/sites/twinspires.com/files/logo-235x98-02.jpg" alt="" style="display: block; width: 235px; height: 98px;" alt="CLUB 1875"> </td> </tr> </table> </center> <center> <table> <tr> <td> <a href="https://itunes.apple.com/us/app/club-1875/id1171549047?mt=8" target="_blank"><img src="https://www.twinspires.com/sites/twinspires.com/files/icon-148x40-02.jpg" alt="Download Here" style="display: block;width:148px;height: 40px; "></a> </td> </tr> </table> </center> <center> <table style="padding-top: 40px;"> <tr> <td> <img src="https://www.twinspires.com/sites/twinspires.com/files/footer-558x278.jpg" style="display: block;width: 558px;height: 278px;" a"

这是我发送电子邮件时的输出:

【问题讨论】:

请帮我解决这个问题.. 我也面临同样的问题 你能发布你到目前为止所做的代码 emailIntent.setType("text/html");,将此行放入您的代码中 意图意图=新意图(意图.ACTION_SEND); // intent.setType("message/rfc822");意图.setType("文本/html"); intent.putExtra(Intent.EXTRA_SUBJECT, "看看我发现了什么!"); intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(mMessage)); startActivity(Intent.createChooser(intent, "发送电子邮件"));我也这样做了,但是没有用...@Bhavnik 【参考方案1】:

试试这个。

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

【讨论】:

那么每次html代码都会一样吗?还是不同? 是的,我在上面发布的内容对于我的每封电子邮件都是一样的。 好的,那么您是否尝试制作HTML 的文件,然后共享该文件? share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getPath())); 也从账户中获取文件 这是我的意图:意图 shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("text/html");文件 file = new File("file:///android_asset/sample.html"); Uri uri = Uri.fromFile(file); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); startActivity(Intent.createChooser(shareIntent, ""));

以上是关于如何在 android 中以 html 格式的电子邮件发送图像?的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中以表格格式显示数据?

在Android中以编程方式发送电子邮件

如何在android中以小写显示文本?

如何在Android中以编程方式向影像添加阴影? [重复]

如何在android中以编程方式将铃声添加到本机应用程序联系人

如何在 PHP 中以 HTML 格式显示 XML?