19

I have generated an HTML code(complete with <html><body></body></html> tags) as a String. Now I want to send this HTML code as HTML to mail. My code is as below.

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"me@mydomain.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "I would like to buy the following");
intent.putExtra(Intent.EXTRA_TEXT, purchaseOrder());
startActivity(Intent.createChooser(intent, "sending mail"));

Where the purchaseOrder() is the method which passes me the string having full HTML code. But though the GMail client opens on my Nexus1 but it has the String with all HTML tags and not the actual HTML view. I tried the following but got error. The GMail crashed.

intent.putExtra(Intent.EXTRA_STREAM, purchaseOrder());
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
JaVadid
  • 7,107
  • 13
  • 42
  • 54

5 Answers5

27

This works for me:

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

But I've notice that inline styles and image tags are being ignored...

Andy Cochrane
  • 2,409
  • 1
  • 18
  • 16
4

For anyone else looking to do this, sending the email manually behind the scenes using android-javamailer works (I've done it):

Sending Emails without User Intervention (no intents) in Android

Sujit
  • 1,653
  • 2
  • 9
  • 25
mdaddy
  • 742
  • 9
  • 10
  • 1
    Yes but you can use your own business email and password to avoid needing the user's information to send an email – Eenvincible Jun 27 '15 at 21:21
  • 2
    how is this secure? even if code is obfuscated i can still search for strings in code as well as inside strings.xml and find username and password. – Timmy Simons Apr 20 '16 at 06:07
1

if i am not wrong what you were looking for was

   Html.fromHtml()

e.g.

Html.fromHtml("<a href="www.google.com"> Google</a>");

this will make Google a hyperlink

Big O
  • 417
  • 1
  • 5
  • 14
1

This worked for me Intent.ACTION_SENDTO and my code goes here:

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..."));
lavan
  • 21
  • 4
1

Try Below Code :

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,getResources().getString(R.string.email_subject));
sendIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(getResources().getString(R.string.email_text)));
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "email"));

string.xml

<resources>    
<string name="email_subject">Download App for your smartphone.</string>
<string name="email_text"><![CDATA[Hey,<br/>I just downloaded App on my Android. 
<br/>It is a smartphone Manager.<br/>App is available for Android.<br/>
    Get it now from http://www.exampleapp.com/download]]></string>