开发者

Javamail and adding link to text

I am using Javamail. Within the MimeMessage.setText, I have to include code that encodes text as a URL. For the purposes like below.

BodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText("Test\n" + text +"\nVisit Test.com");`

In this I need Test.com to be embedded as a URL. Is there a tag or wildcard that would do this? Thanks.

Bas开发者_开发问答ically I would prefer to avoid using html in the javamail and utilizing the following.

Test.com


If you want the link to be clickable in the mail, you should send the mail as HTML.

To do this, you should try to create an HTML MIME mail:

InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-type", "text/html; charset=UTF-8");
String html = "Test\n" + text + "\n<a href='http://test.com'>Test.com</a>";
MimeBodyPart body = new MimeBodyPart(headers, html.getBytes("UTF-8"));

EDIT:

It is also possible to use setText when sending HTML-mail:

String html = "Test\n" + text + "\n<a href='http://test.com'>Test.com</a>";
messageBodyPart.setText(html, "UTF-8", "html");

See the the API for more details


A couple of things need to happen for this to work.

  1. Send the link as html
  2. Set the content type to text/html

String text = "Test\n" + text +"\nVisit <a href="http://test.com">Test.com</a>"; messageBodyPart.setContent(text, "text/html");

From JavaMail API FAQ

Q: How do I send HTML mail? A: There are a number of demo programs included with the distribution that show how to send HTML mail. If you want to send a simple message that has HTML instead of plain text, see the sendhtml.java program in the demo directory. If you want to send an HTML file as an attachment, see the sendfile.java example that shows how to send any file as an attachment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜