37

i found a way to send plain text email using intent:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain"); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new     
String[]{"example@mail.com"}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Test");

But I need to send HTML formatted text.
Trying to setType("text/html") doesn't work.

Jonik
  • 80,077
  • 70
  • 264
  • 372
Denis Palnitsky
  • 18,267
  • 14
  • 46
  • 55

6 Answers6

51

You can pass Spanned text in your extra. To ensure that the intent resolves only to activities that handle email (e.g. Gmail and Email apps), you can use ACTION_SENDTO with a Uri beginning with the mailto scheme. This will also work if you don't know the recipient beforehand:

final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject");
shareIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
    .append("<p><b>Some Content</b></p>")
    .append("<small><p>More content</p></small>")
    .toString())
);
antnerves
  • 658
  • 6
  • 4
  • Has anybody tested that solution? I have no possibility to check it now so please add a comment if it works and I will mark this as right answer. – Denis Palnitsky Apr 27 '11 at 15:42
  • 8
    @Orsol, Yes this works sort of. If you want formatted email, this does work, however I wanted to add a link, which unfortunately did not work:( – Phil Jun 01 '11 at 21:56
  • you can add link using the following `shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder() .append("

    https://example.com").toString()) );`
    – glo Jan 16 '13 at 12:37
  • 1
    glo the problem with that is, while it works with gmail, it doesn't work with every email client. So if you need that link in there and someone other then gmail can grab it then you cannot use your solution. – MinceMan Aug 01 '13 at 18:08
  • 3
    Agreed. This works if the user picks Gmail to send the message. But the standard Android Mail app just drops the HTML formatting including links. There should be a way to provide both HTML and fallback text for the client. But I don't know how. – Andrew Arnott Jan 21 '14 at 07:22
  • 3
    I noticed that The tag does not work in both GMAIL app and the standard Android Email app. I mean it does not get formatted as a table.
    – superlinux May 17 '14 at 08:50
  • 2
    Html.fromHtml is now deprecated in Android N+ – Daniele D. Mar 21 '18 at 17:20
  • 1
    Over the years unfortunately all answers about this seem to be outdated. – cV2 Sep 07 '18 at 16:47
  • gmail app seems to be ignoring most tags, including – hmac May 13 '20 at 14:06
4

Been trying to send html via gmail app for a while, so decided to leave some insight on what I found, just in case someone else is having similar issues.

Seems like no matter what I did, I couldn't get the html to have bold text in it. Then I've tried switching to outlook client and to my surprise it was working just fine. Html markup was also working on other older devices, but not on mine (galaxy s7 API 26), so I figured, that gmail app seems to have dropped support for html syntax that comes from intent or maybe now you're required to provide it in some very specific way which is not clearly documented.

Last gmail version that worked for me was version 6.9.25... on Nexus 5X API 25 emulator (Nougat) And it stopped working starting version 7.5.21... On Nexus 5x API 26 emulator (Oreo)

SMGhost
  • 3,867
  • 6
  • 38
  • 68
3

This was very helpful to me for the HTML, but the ACTION_SENDTO didn't quite work for me as is - I got an "action not supported" message. I found a variant here which does:

http://www.coderanch.com/t/520651/Android/Mobile/no-application-perform-action-when

And here's my code which combines the two together:

String mailId="yourmail@gmail.com";
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, 
                                Uri.fromParts("mailto",mailId, null)); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here"); 
// you can use simple text like this
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here"); 
// or get fancy with HTML like this
emailIntent.putExtra(
         Intent.EXTRA_TEXT,
         Html.fromHtml(new StringBuilder()
             .append("<p><b>Some Content</b></p>")
             .append("<a>http://www.google.com</a>")
             .append("<small><p>More content</p></small>")
             .toString())
         );
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Andy Weinstein
  • 2,639
  • 3
  • 21
  • 32
  • 4
    Have you tested this ? This looks fine in the native android email client on the emulator (not Gmail) but text does not appear styled when sent to a gmail account. In addition, the linked URL you give isn't visible. – tos Feb 27 '12 at 16:48
2

I haven't (yet) started Android development, but the documentation for the intent says that if you use EXTRA_TEXT, the MIME type should be text/plain. Seems like if you want to see HTML, you'd have to use EXTRA_STREAM instead...

Chris Shaffer
  • 32,199
  • 5
  • 49
  • 61
  • This works partly. I attached file but it MIME type always text\plain in resulting letter. I tried to set emailIntent.setType("html/text"); with no luck. – Denis Palnitsky Jan 06 '10 at 08:37
-1

You must to change "EXTRA_TEXT" for "EXTRA_HTML_TEXT"

https://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT

  • It says EXTRA_TEXT is required (_Note that you must also supply EXTRA_TEXT_). Yet this doesn't work for email. – Daniel F Jun 17 '19 at 15:02
-3

What about just trying to add some html in the text area?

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "<strong>Test</strong>");
EpicDewd
  • 377
  • 1
  • 4
  • 8