17

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

enter image description here enter image description here

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

enter image description here enter image description here

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

enter image description here

Community
  • 1
  • 1
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • @Hesam I am not able to achieve the 1st one. I tried pasting your complete code (1st one) and links are displayed as plain text. Any thoughts? – SKP Aug 02 '17 at 18:27
  • @Hesam have you found solution? if you got one does it support with html table tags? Can you post the solution if you got one. Thanks – SARATH V Dec 13 '17 at 06:43

3 Answers3

5

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.

Lee
  • 3,996
  • 3
  • 33
  • 37
  • I found this solution doesn't work, though it does filter the apps to only email types, it ends up with same problem originally stated, Gmail doesn't properly format the text into html. – gnichola Sep 16 '13 at 20:57
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

user987171
  • 122
  • 5
  • Thanks, I tried your suggestion. Although it opens mail client, it doesn't have my assigned data such as to and body. both of them are clean. – Hesam Jul 12 '12 at 09:45
2

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

pareshgoel
  • 981
  • 8
  • 12