Android,如何发送HTML电子邮件并强制Android通过G-Mail发送不是其他应用程序? [英] Android, How to send HTML email and force Android to send it through G-Mail not other applications?
问题描述
1)使用Intent(Intent.ACTION_SEND)。这是非常简单的方式,我可以看到我的身体以HTML格式,但问题是当我点击发送电子邮件按钮,所以许多应用程序,如Facebook和Google+弹出,这是无用的,我不应该显示在该列表。这是它的代码:
String html =<!DOCTYPE html>< html>< body>< a href = \http://www.w3schools.com\target = \_blank\>访问W3Schools.com!< / a> +< p>如果将目标属性设置为\_blank\,链接将在新的浏览器窗口/选项卡中打开。< / p>< / body>< / html>;
意图意图=新意图(Intent.ACTION_SEND);
intent.setType(text / plain);
intent.putExtra(Intent.EXTRA_EMAIL,new String [] {我的电子邮件地址});
intent.putExtra(Intent.EXTRA_SUBJECT,subject here);
intent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(html));
startActivity(Intent.createChooser(intent,发送电子邮件...));
2)使用Intent(Intent.ACTION_SENDTO)。这样过滤无用的应用程序,并向我显示邮件客户端。但是,它不会在Gmail中显示HTML格式的电子邮件。当我发送电子邮件时,一些客户端以HTML格式显示正文,而其他客户不识别HTML,并且我的链接行为像纯文本。此代码如下:
String html =<!DOCTYPE html>< html>< body>< a href = \http://www.w3schools.com\target = \_blank\>访问W3Schools.com!< / a> +< p>如果将目标属性设置为\_blank\,链接将在新的浏览器窗口/选项卡中打开。< / 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\>访问W3Schools.com!< / a> +< p>如果将目标属性设置为\_blank\,链接将在新的浏览器窗口/选项卡中打开。< / 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 ...));
尝试以下 -
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);
这只会提供电子邮件应用程序。
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.
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:
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:
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 API which adds so much complexity to application and I didn't test it so far.
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.
any suggestion would be appreciated. Thanks
======================
Update
Something about code 2 is wrong. The code is like this:
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..."));
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.
这篇关于Android,如何发送HTML电子邮件并强制Android通过G-Mail发送不是其他应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!