Embed html table tag in email intent android

2019-07-30 04:33发布

问题:

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.

回答1:

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/



回答2:

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?