我想通过我的应用程序发送电子邮件。我需要发送基于HTML的电子邮件只是通过G-Mail。我发现下面的解决方案,每个方案都有优缺点。
1)使用Intent (Intent.ACTION_SEND)。这是一种非常简单的方式,我可以用HTML格式看到我的正文,但问题是当我点击“发送电子邮件”按钮时,会弹出许多像Facebook和Google+这样的应用程序,它们都是无用的,我不应该在列表中显示它。下面是它的代码:
String html = "<!DOCTYPE html><html><body><a href=\"http://www.w3schools.com\" target=\"_blank\">Visit W3Schools.com!</a>" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"MY EMAIL ADDRESS"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(html));
startActivity(Intent.createChooser(intent, "Send email..."));
2)使用Intent (Intent.ACTION_SENDTO)。这种方式过滤了无用的应用程序,只显示了邮件客户端。但是它不能在gmail客户端以HTML格式显示我的电子邮件。当我发送电子邮件时,一些客户端以HTML格式显示正文,而另一些客户端无法识别HTML,并且我的链接表现得像纯文本。此代码如下所示:
String html = "<!DOCTYPE html><html><body><a href=\"http://www.w3schools.com\" target=\"_blank\">Visit W3Schools.com!</a>" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:MY EMAIL ADDRESS" + "?subject=subject here" + "&body=" + html;
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
3)使用JavaMail API发送邮件,这给应用程序增加了太多的复杂性,到目前为止我还没有测试它。
你有什么建议吗?我需要一种方法来获得第一种方法和第二种方法的优势。我需要当用户点击按钮时,它显示Gmail客户端,我可以在客户端的主体部分显示他/她的html内容。
任何建议都将不胜感激。谢谢
======================
更新
代码2有些地方是错误的。代码如下:
String html = "<!DOCTYPE html><html><body><a href=\"http://www.w3schools.com\" target=\"_blank\">Visit W3Schools.com!</a>" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:MY EMAIL ADDRESS" + "?subject=subject here" + "&body=" + Html.fromHtml(html);
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
https://stackoverflow.com/questions/11447445
复制相似问题