Android, How to send HTML email and force Android

2019-01-22 20:09发布

问题:

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..."));

回答1:

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.



回答2:

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



回答3:

Try this code: It will select only email providers, not facebook etc.

 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:"));


回答4:

To get only email applications, use Intent.setType("message/rfc822")