使用Android意图发送HTML邮件

时间:2010-03-30 09:37:00

标签: html android email android-intent

我已经生成了一个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[]{"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"));

purchaseOrder()是向我传递具有完整HTML代码的字符串的方法。但是,虽然GMail客户端在我的Nexus1上打开,但它包含所有HTML标记的String,而不是实际的HTML视图。我试过以下但是错了。 GMail坠毁了。

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

5 个答案:

答案 0 :(得分:27)

这对我有用:

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

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

答案 1 :(得分:4)

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

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

答案 2 :(得分:1)

如果我没错,那你要找的是

   Html.fromHtml()

e.g。

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

这将使Google成为超链接

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

尝试以下代码:

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

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