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.
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.
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()) );
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..."));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With