CSS not working while Sending HTML email via Intent in android - android

I want to send Html via mail app, problem is with CSS is not working when I am using Gmail to compose/send mail
And here is the intent spinet
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Summary");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<div style='background: red;'>TEST</div>"));
intent.putExtra(Intent.EXTRA_HTML_TEXT, "<div style='background: red;'>TEST</div>");
intent.setType("text/html");
startActivity(Intent.createChooser(intent, "Send mail"));
Even I applied inline CSS its not working, Can Any help what's wrong am I doing

Gmail cuts off the head, so the CSS gets lost. For HTML Emails, you should always use inline styles.
Edit:
The general rule to use inline styles is still true, but in your specific case, it seems to be an issue with sending a HTML Mail with an Intent. I found in another answer on SO, that this may isn't possible.

Related

Android: Send HTML text using ACTION_SEND Intent

I'm trying to send some HTML formatted text via the ACION_SEND Intent in my app. The HTML file is located in my assets directory and it has links to some images and contains other CSS stylings. Thus far I've tried the following:
private Intent getEmailIntent(String deepLink) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// sharingIntent.setType("text/html");
sharingIntent.setType("text/plain");
// sharingIntent.setType("message/rfc822");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getShareSubject());
sharingIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(getHtmlTextFromAssetsFile("myHtml.html")));
return sharingIntent;
}
As you can see from the above method, I've tried using all three of the IntentTypes in the code, without success.
The HTML file has CSS within a <style> tag , and that is not applied. It just shows up as plain text in all cases. No images (remotely located) are loaded. And </br> tags are ignored.
Has anyone worked with this? How can I get my app to send such an email? Any pointers would be helpful.
Probably not many email clients on Android support HTML. What you can do is providing plain text in EXTRA_TEXT and putting the HTML in EXTRA_HTML_TEXT as documented here: https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND

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 to send html content with image through android default email client?

How can we get inline images in default email client for android through Hypertext Markup Language (HTML) ?
You can not. As default Email application's doesn't support <img /> tag.
Because, ImageSpan doesn't implementing Parcelable.
Hence its failed with Intent's PUT_EXTRA.
Its works only for basic tags, like, <b>, <i> ..etc
Look at Sending html email in android using <table>, etc. - is there really no relatively built-in Intent way? and How to show an image in the email body?
String body ="<html><body><table>...</table></body></html>";
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).toString());
startActivity(Intent.createChooser(emailIntent, "Email:"));
Unfortunately, the <table> ,'img' tag isn't supported. Supported tags are actually more dependent on the email client you use to send the email - some of them are more finicky/restrictive than others. Most of them use the super-basic formatting tags like <b>, <i>, <h1>, and so on, though.

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[]{"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>");

Categories

Resources