使用 Android Intent 发送 HTML 邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2544141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:16:35  来源:igfitidea点击:

Send HTML mail using Android intent

htmlandroidemailandroid-intent

提问by JaVadid

I have generated an HTML code(complete with <html><body></body></html>tags) as a String. Now I want to send this HTML code as HTML to mail. My code is as below.

我生成了一个 HTML 代码(完整的<html><body></body></html>标签)作为字符串。现在我想将此 HTML 代码作为 HTML 发送到邮件。我的代码如下。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
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.

其中purchaseOrder()是将具有完整 HTML 代码的字符串传递给我的方法。但是,尽管 GMail 客户端在我的 Nexus1 上打开,但它具有包含所有 HTML 标签的字符串,而不是实际的 HTML 视图。我尝试了以下但出错了。GMail 崩溃了。

intent.putExtra(Intent.EXTRA_STREAM, purchaseOrder());

回答by Andy Cochrane

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

但我注意到内联样式和图像标签被忽略了......

回答by mdaddy

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

对于其他想要这样做的人,请使用 android-javamailer 在幕后手动发送电子邮件(我已经完成了):

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

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

回答by Big O

if i am not wrong what you were looking for was

如果我没看错的话,你要找的是

   Html.fromHtml()

e.g.

例如

Html.fromHtml("<a href="www.google.com"> Google</a>");

this will make Google a hyperlink

这将使谷歌成为一个超链接

回答by lavan

This worked for me Intent.ACTION_SENDTOand my code goes here:

这对我有用Intent.ACTION_SENDTO,我的代码在这里:

String mailId="[email protected]";
    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..."));

回答by Mohd. Jafar Iqbal khan

Try Below Code :

尝试以下代码:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,getResources().getString(R.string.email_subject));
sendIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(getResources().getString(R.string.email_text)));
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "email"));

string.xml

字符串.xml

<resources>    
<string name="email_subject">Download App for your smartphone.</string>
<string name="email_text"><![CDATA[Hey,<br/>I just downloaded App on my Android. 
<br/>It is a smartphone Manager.<br/>App is available for Android.<br/>
    Get it now from http://www.exampleapp.com/download]]></string>