简体   繁体   中英

How to send html mail using default mail in android?

I am trying to send html mails using Intent.In the composer it shows in html format but in the recipient end, it shows as normal text.I have to send image and text with hyperlink. The screen shot of the composer looks like this

在此处输入图片说明

Following is what I have tried so far,

public class Sendingamail extends Activity {
    /** Called when the activity is first created. */
    Button send;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    send=(Button) findViewById(R.id.emailsendbutton);
    send.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                            // TODO Auto-generated method stub

                                  final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                                  emailIntent.setType("text/html");
                             String html = "<!DOCTYPE html><html><body><a href=\"http://www.google.com\">Visit W3Schools.com!</a>" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
                                  emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hhhhhhhh");

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(html));

 Sendingamail.this.startActivity(emailIntent);

                                  });
}
}

This works for me:

  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())
    );
String text = "<body><b>" +heading +
                "<br>..............................</b><br><br>" +
                "<b><font color='green'>Posted Date :</font></b><br>"+ postedDate + "<br><br>"
                + "<b><font color='green'>Posted By :</font></b><br>" + postedBy+ "<br><br>"
                + "<b><font color='green'>Description :</font></b><br>"+ desc + "<br><br>" 
                + "<b><font color='green'>Post type :</font></b><br>"
                + postType + "</body>";


                    sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(text));
                    startActivity(sendIntent);

I ran into the same problem few days back. There's nothing much you can do in code about it because the html content will only be displayed if the mail client you are using is able of handling all html and css stuff which the default email and gmail client is not capable of. So eighter you can use the java mail api to send html mail or you will have to look for a mail client who supports full html. Please have a look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM