Android,如何发送 HTML 电子邮件并强制 Android 通过 G-Mail 而不是其他应用程序发送它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11447445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android, How to send HTML email and force Android to send it through G-Mail not other applications?
提问by Hesam
I want to send email through my application. I need to send HTML based email just through G-Mail. I found following solutions that each of them has pros and cons.
我想通过我的应用程序发送电子邮件。我只需要通过 G-Mail 发送基于 HTML 的电子邮件。我发现以下解决方案各有利弊。
1) Using Intent (Intent.ACTION_SEND). This is very simple way and I can see my body in HTML format but the problem is when I click on "Send email" button, so many applications like Facebook and Google+ pop up which are useless and I shouldn't show it in that list. This is its code:
1)使用意图(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) Using Intent (Intent.ACTION_SENDTO). This way Filters useless applications and shows me just mail clients. But it doesn't display my email in HTML format in gmail client. When i send the email some clients show the body in HTML format while others doesn't identify HTML and my link behaves like plain text. This code is like:
2)使用意图(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) Sending mail using JavaMail APIwhich adds so much complexity to application and I didn't test it so far.
3) 使用JavaMail API发送邮件,这给应用程序增加了太多的复杂性,到目前为止我还没有对其进行测试。
What is your suggestion? I need a way to have advantages of first and second ways. I need when user click on button it shows Gmail client and I can show him/her html content in body part of client.
你的建议是什么?我需要一种方法来拥有第一种和第二种方式的优势。我需要当用户点击按钮时它会显示 Gmail 客户端,我可以在客户端的正文部分显示他/她的 html 内容。
any suggestion would be appreciated. Thanks
任何建议将不胜感激。谢谢
======================
======================
Update
更新
Something about code 2 is wrong. The code is like this:
关于代码 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..."));
回答by Lee
Try the following -
尝试以下 -
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);
This will only present email applications.
这只会显示电子邮件应用程序。
回答by pareshgoel
To get only email applications, use Intent.setType("message/rfc822")
要仅获取电子邮件应用程序,请使用 Intent.setType("message/rfc822")
回答by user987171
If you want only one application to handle your intent then you need to remove Intent.createChooser(), rather jst use startActivity()---> it send the mail using default email client, if not set then will ask to do so... tat can be changed anytime
如果您只想要一个应用程序来处理您的意图,那么您需要删除 Intent.createChooser(),而不是使用 startActivity()---> 它使用默认电子邮件客户端发送邮件,如果未设置,则将要求这样做。 .. tat 可以随时更改
回答by Manikandan
Try this code: It will select only email providers, not facebook etc.
试试这个代码:它只会选择电子邮件提供商,而不是 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:"));