Java Mail嵌入URL

时间:2016-02-12 11:26:04

标签: java jsp javamail scriptlet

当我在电子邮件正文中添加HTML网址时,它不会重定向到首选位置。这是片段,请告诉我我做错了什么。

#location variable contains the URL
StringBuffer body = new StringBuffer("<html><body>Hi, <br/><br/>");
body.append("<p>"+cmts+"</p>");

#both the ways are not working, how to construct proper URL 
body.append("<br/><br/><a href=\"+location+\">" + location + "</a><br/>");
body.append("<br/><br/><a href="+location+">" +location + "</a><br/>");

#this is working as link only in OUTLOOK, but in other mail client it shows as plain text
body.append("<br/><br/>"+location);

网址:

  

http://host:port/weebApp/report/viewer.html#%2Fpublic%2FSamples%2FDashboards%2_FSample_report

2 个答案:

答案 0 :(得分:0)

有很多方法可以在javamail中添加href,例如:

1)InternetHeaders headers = new InternetHeaders(); headers.addHeader("Content-type", "text/html; charset=UTF-8"); String aHref = "some text\n" + text + "\n<a href='http://google.com'>google.com</a>";

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

更新:
确保内容类型设置为htmltext/html,因为text/plain会将其显示为仅文字

答案 1 :(得分:0)

看起来像是引号的问题。尝试:

body.append("<br/><br/><a href=\""+location+"\">" + location + "</a><br/>");