Hes*_*sam 17 email android send android-intent
我想通过我的申请发送电子邮件.我需要通过G-Mail发送基于HTML的电子邮件.我找到了以下解决方案,每个解决方案都有利有弊.
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..."));
Run Code Online (Sandbox Code Playgroud)
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..."));
Run Code Online (Sandbox Code Playgroud)
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..."));
Run Code Online (Sandbox Code Playgroud)
尝试以下方法 -
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);
Run Code Online (Sandbox Code Playgroud)
这只会呈现电子邮件应用程序.
小智 2
如果您只想一个应用程序来处理您的意图,那么您需要删除 Intent.createChooser(),而只是使用 startActivity()---> 它使用默认电子邮件客户端发送邮件,如果未设置,则会要求这样做。 .. tat 可以随时更改