How to send HTML email - android

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.

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())
);

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)

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

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...

You must to change "EXTRA_TEXT" for "EXTRA_HTML_TEXT"
https://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT

What about just trying to add some html in the text area?
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "<strong>Test</strong>");

Related

EXTRA_SUBJECT not getting data

I'm trying to send an Email via an Android App. Currently using Android Studio 1.2 and testing on Genymotion 2.4.0 on a Galaxy Nexus 4.3(API 18) image with ARM Translation and Google apps installed. I know this question is very similar to this, but I've tried every suggestion there to no effect.
I can retrieve the email(where to send) and the "body" of the email, but unnable to get the email Subject. Code is as follows:
Intent iEmail = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "email#somewhere.com", null));
iEmail.setType("text/plain");
// Both these "options" don't work
iEmail.putExtra(Intent.EXTRA_SUBJECT, etTema.getText());
iEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
// This works fine
iEmail.putExtra(Intent.EXTRA_TEXT, etDesc.getText());
startActivity(Intent.createChooser(iEmail, "Choose:"));
When the Activity starts and I choose the email client email(to send to) is displayed correctly as is the "body" but the subject remains blank.
EDIT: result is always the same whether i use the EditText(etTema) or try passing a string.
Any suggestions? Thanks
Your code should work, but I remember using:
String uriText = "mailto:youremail#gmail.com" +
"?subject=" + Uri.encode("some subject text here") +
"&body=" + Uri.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email"));
See if that works better across devices / APIs for you.
Try just doing this. This works great for me.
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "email#somewhere.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, etDesc.getText());
startActivity(Intent.createChooser(emailIntent, "Send Email"));

Embed html table tag in email intent android

I am working on a concept in which I have to embed html table tag in email body.I tried some work around code it's working fine in 2.2 and 2.3 OS versions but the same code is not working in latest OS versions like 4.0 etc.
I also tried Spannable and Html.fromHtml()but it's also not working.
Here is my code which is working on 2.2 and 2.3 OS versions:
//SEND EMAIL
private void sendEmail(String imageUri) throws WriterException{
//....................Prepare share email data.............................
int itemNumber=0;
titleBuffer=new StringBuffer();
titleBuffer.append("<html><body><table border="+"1"+"><tr border="+"0"+"><th>Item number</th>"+
"<th>Barcode</th>"+"<th>Product name</th>"+"<th>Quantity</th></tr>");
for(int i=0;i<_productList.size();i++){
itemNumber=i+1;
titleBuffer.append("<tr border="+"0"+"><td>"+itemNumber+"</td>"+
"<td>"+_productList.get(i).getProductBarcode()+"</td>"+"<td>"+_productList.get(i).getProductName()+"</td>"+
"<td>"+_productList.get(i).getProductQuantity()+"</td></tr>");
/*titleBuffer.append("<tr border="+"0"+"><td>"+itemNumber+"</td>"+
"<td>"+_productList.get(i).getProductBarcode()+"</td>"+"<td>"+_productList.get(i).getProductName()+"</td>"+
"<td>"+_productList.get(i).getProductQuantity()+"</td>"+"<td>"+
"<img src="+"/'data:image/png;base64, "+generateBarcodeImage(GenerateBarcode.encodeAsBitmap(_productList.get(i).getProductBarcode(),
BarcodeFormat.CODE_128, 600, 300))+"/'/></td></tr>");*/
}
titleBuffer.append("</table></body></html>");
SpannedString bar = new SpannedString(titleBuffer.toString());
if(_productList.size()==0){
titleBuffer.append("NA");
}
//..........................................................................
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Shopping List");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,titleBuffer.toString());
emailIntent.putExtra(Intent.EXTRA_STREAM, emailImageUri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
Also find the attached image which proves it worked in 2.2 and 2.3 OS versions.
It depends on the email client which is handling it. Not all the email clients supports all the HTML tags.Atleast Gmail doesn't support it. Its works only for basic tags, like, <b>, <i> etc.It doesn't work for especially <img> tag.
See these so posts for more info,
Sending html email in android using <table>, etc. - is there really no relatively built-in Intent way?
Android, How to send HTML email and force Android to send it through G-Mail not other applications?
How to send html content with image through android default email client?
try below code :-
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())
);
for more info see below link:-
android - How to format the text as table in email body of email client
How to send HTML email
http://blog.iangclifton.com/2010/05/17/sending-html-email-with-android-intent/

sending html mail if app allows

I want to send a html mail from my application.
I know that not all mail clients allow html tags.
But I found the constant Intent.EXTRA_HTML_TEXT in the API (http://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT).
My code looks like this, but it shows always just the text and not the html formatted text whatever mail client I use:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, "Hello World");
intent.putExtra(Intent.EXTRA_HTML_TEXT, "<html><body><h1>Hello World</h1></body><html>");
intent.setType("text/html"); // intent.setType("plain/text");
startActivity(Intent.createChooser(intent, "Choose Email Client:"));
So where is the mistake?
Sorry, not a positive answer because it doesn't seem to work, at least not in a way that's really universal and reliable. Some mailers are happy with this:
String body = "<html>something</html>";
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
Note that you don't need the new EXTRA_HTML_TEXT, it works with the older one as well. If this covers all you need then you might be OK. But if you also want to address many other possible intent receivers like Facebook, Skype or even apps like Drive or Keep, unfortunately, I couldn't find a perfect solution but I'd very much like to be proven wrong.
Basically, we have three different formats:
String body = "<html>something</html>";
Spanned html = Html.fromHtml(body);
String stripped = html.toString();
and two possible recipients:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
intent.putExtra(Intent.EXTRA_HTML_TEXT, ???);
intent.putExtra(Intent.EXTRA_TEXT, ???);
I tried all possible combinations but in any of those, there will be some well known and widely used app that doesn't want to play nicely. Either we get HTML tags embedded, or no formatting, or even no text at all...

Send HTML e-mail with Intent

I want to send a HTML e-mail when an user decides to share my app. I'm using HTML in order to have a customised and appellative message.
First approach, I tried to create a String with my HTML (inline style) using Html.fromHtml but when I received the e-mail it was pure txt, no customization.
Second approach, send a HTML file attached. The problem with this approach is that the HTML is not showed until the user opens the attach.
What's the best solution, is it possible?
Thanks!
You can achieve your task using this method Html.fromHtml(String);
Html.fromHtml("<font color='#ff123456'>text</font>")
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())
);

How do I add a correctly formatted link (with a hash mark) to an email message using intent on Android?

I am creating an email intent and filling the message with a link that contains a hashmark:
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://en.wikipedia.org/wiki/Android_(operating_system)#Software_development");
Once I send this and reopen it on Android, the default mail client only makes the link to http://en.wikipedia.org/wiki/Android_(operating_system)
Is there a way to fix this problem?
This is a problem with Android's default mail. Links work great using the gmail app!
Try the following. I am unable to test my code because of problem in my IDE.
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("http://en.wikipedia.org/wiki/Android_(operating_system)#Software_development"));
Thanks
Deepak

Categories

Resources