我想通过我的应用程序发送电子邮件。我需要发送基于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..."));
发布于 2012-10-25 00:30:49
尝试以下操作-
Intent shareIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(body));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
startActivity(shareIntent);
这将仅显示电子邮件应用程序。
发布于 2012-07-12 16:52:47
如果你只想要一个应用程序来处理你的意图,那么你需要移除Intent.createChooser(),而不是使用startActivity()->它使用默认的电子邮件客户端发送邮件,如果没有设置,那么会要求这样做...tat可以随时更改
发布于 2012-07-12 17:23:09
试试这个代码:它将只选择电子邮件提供商,而不是facebook等。
String body="<!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>";
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
startActivity(Intent.createChooser(emailIntent, "Email:"));
https://stackoverflow.com/questions/11447445
复制相似问题