如何在Android中的HTML电子邮件中包含图像

时间:2012-02-23 06:09:53

标签: android html-email

我正在尝试使用HTML在我的电子邮件正文中发送图片,但图片没有发送。我不知道我做错了什么。我尝试使用以下代码:

Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("text/html");
String tempStr = "<html><head><title></title><style> b{ color: red}             </style></head><body><img src=\""+ "images/assaultoffences.png" + "\"><b>quick</b></body>   </html>";
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(tempStr));
i.putExtra(Intent.EXTRA_EMAIL, new String[] {"ashwini.soma@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
startActivity(i);

try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(SavePage.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

4 个答案:

答案 0 :(得分:1)

这种令人沮丧的似乎并没有在Android中本地支持。一个解决方法是使用Java Mail并自己弄清楚。我所做的是使用droidQuery库帮助将我的HTML写入文件并将其附加到电子邮件中:

final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
String html = "<div>Place Your HTML here.</div>";
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(html));
shareIntent.setType("text/html");
$.with(this).write(html, FileLocation.EXTERNAL, "email.html", false, false, new Function() {

    @Override
    public void invoke($ droidQuery, Object... params) {
        String file = ((File) params[1]).getAbsolutePath();
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
        startActivity(shareIntent);
    }
}, new Function() {
    @Override
        public void invoke($ droidQuery, Object... params) {
            Log.e("$", (String) params[1]);
        }
});

然后在html <body>的顶部添加了此块(在Android电子邮件中格式也很差):

<div style="background: gray; color: black; align='center'" >
    Trouble viewing this email? Open the attachment to view in your web browser.
</div>

如果您正在考虑第一种方法(Java Mail),我建议使用droidQuery {{1}}扩展名,{{1}}使用该库提供简化的API。

答案 1 :(得分:0)

图像需要托管在某处。当用户收到电子邮件时,客户端将查找不存在的图像“images / assaultoffences.png”。 将其托管在某处,然后在电子邮件中指定完整的URL,例如http://mydomain.com/images/assaultoffences.png

答案 2 :(得分:0)

嘿,您可以使用this参考作为解决方案。

我希望这有助于您解决问题。

感谢。

答案 3 :(得分:0)

仅仅因为html引用图像并不意味着电子邮件应用程序将读取html,解析它,查找引用的图像,将它们附加到电子邮件中,并将引用更改为内部。

已经有question and answer描述了在发送电子邮件时如何将文件附加到电子邮件中。

This answer向您展示如何从html引用附加图像,使其显示在正文中(至少适用于Outlook)。