Send HTML mail using Android intent

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I have generated an HTML code(complete with 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()); 

回答1:

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



回答2:

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

http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android



回答3:

if i am not wrong what you were looking for was

   Html.fromHtml() 

e.g.

Html.fromHtml(" Google"); 

this will make Google a hyperlink



回答4:

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("

Some Content

") .append("http://www.google.com") .append("

More content

") .toString()) ); startActivity(Intent.createChooser(emailIntent, "Send email..."));


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!