Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send HTML email

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[]{"[email protected]"});  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.

like image 915
Denis Palnitsky Avatar asked Jan 05 '10 16:01

Denis Palnitsky


People also ask

Can you send HTML files via email?

The most important thing to know about HTML email is that you can't just attach an HTML file and a bunch of images to a message, then click send. Most of the time, your recipient's email application will break all the paths to your image files by moving your images into temporary folders on the recipient's hard drive.


2 Answers

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()) ); 
like image 84
antnerves Avatar answered Sep 19 '22 16:09

antnerves


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="[email protected]"; 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...")); 
like image 25
Andy Weinstein Avatar answered Sep 20 '22 16:09

Andy Weinstein