John
John

Reputation: 1888

Send email intent with image in html body

Is it possible to send an email, using an intent, with an image in the body. I'm using an image that is hosted...

public void sendEmail(View view){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/html");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Image in body test");

    intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(getHtmlBody()));
    startActivity(Intent.createChooser(intent, "Send Email"));
}

private String getHtmlBody() {
    String html = "<h1><img width=\"100\" src=\"http://cdn2-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-8.jpg\">   Hello World </h1>";
    return html;
}

I am able to do this using javaMail but that sends the email automatically without the user being able to see anything so I'm hoping I can use intents.

Upvotes: 0

Views: 3474

Answers (2)

cketti
cketti

Reputation: 1377

There is EXTRA_HTML_TEXT that allows you to supply HTML as alternative to the text in EXTRA_TEXT. However, there's no guarantee that the receiving app supports this extra (hence the requirement that EXTRA_TEXT must be present, too).

The code could look something like this:

public void sendEmail(View view){
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.setType("text/plain");
  intent.putExtra(Intent.EXTRA_SUBJECT, "Image in body test");
  intent.putExtra(Intent.EXTRA_TEXT, getTextBody());
  intent.putExtra(Intent.EXTRA_HTML_TEXT, getHtmlBody());

  startActivity(Intent.createChooser(intent, "Send Email"));
}

private String getHtmlBody() {
  return "<h1><img width=\"100\" src=\"http://cdn2-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-8.jpg\">"
      + getTextBody() + "</h1>";
}

private String getTextBody() {
  return "Hello world";
}

Apps that don't support HTML will simply use the text from EXTRA_TEXT.

Upvotes: 1

ClayHerendeen
ClayHerendeen

Reputation: 197

The answer by Arpit Garg in this post shares that and tags do not work in most email clients, so unfortunately your code wont work. I just tested your code with the Gmail app and a few others and it pulled in a placeholder but not the actual image. what you can do though is add an image as an attachment.

Upvotes: 0

Related Questions