我正在研究Java RestFul WebProjects。在我的项目中,我有一个使用JavaMail.java类的util包。在这个项目中,我向我们的需求发送邮件,为了这个目的,我在我的String body变量中添加了HTML,并传递了我的html代码。
我的疑问是,我需要将html代码放在本地主机中的一个文件中,并且我需要给出字符串body变量的html文件路径。有人能帮我吗?
在这里,我放置了我的JavaMail.Java类代码:
public static void sendEmailForProfileActivation(int activationCode, String to) throws AddressException, MessagingException {
/**
* Sender's credentials
* */
String from = "helloworld@gmail.com";
String password = "888888";
String sub = "Activate Your Profile using activation code";
//String body = "Activate Profile Using the active Code: <b>" + activationCode + "</b>.";
String body="<html>"
+ "<body>"
+ "<table width=\"100%"+"\" cellpadding=\"0"+"\" cellspacing=\"0"+"\" bgcolor=\"e4e4e4"+"\">"
+ "<tr>"
+ "<td>"
+ "<table id=\"top-message"+"\" cellpadding=\"20"+"\" cellspacing=\"0"+"\" width=\"600"+"\" align=\"center"+"\">"
+ "<tr>"
+ "<td align=\"center"+"\">"
+ "<p>Trouble viewing this email? <a href=\"#"+"\">View in Browser</a></p>"
+ "</td>"
+ "</tr>"
+ "</table><!-- top message -->"
+ "<table id=\"main"+"\" width=\"570"+"\" align=\"center"+"\" cellpadding=\"0"+"\" cellspacing=\"15"+"\" bgcolor=\"ffffff"+"\">"
+ "<tr>"
+ "<td>"
+ "<table id=\"header"+"\" cellpadding=\"10"+"\" cellspacing=\"0"+"\" align=\"center"+"\" bgcolor=\"8fb3e9"+"\">"
+ "<tr>"
+ "<td width=\"570"+"\" bgcolor=\"#7EB646"+"\">"
+ "<h1><font color=\"white"+"\">HelloWorld Services 15 July 2015</font></h1>"
+ "</td>"
+ "</tr>"
+ "<tr>"
+ "</tr>"
+ "</table><!-- header -->"
+ "</td>"
+ "</tr><!-- header -->"
+ " <!--tr>"
+ "<td height=\"30"+"\">"
+ "<img src=\"http://dummyimage.com/570x30/fff/fff"+"\" />"
+ "</td>"
+ "</tr For spacing pupose one image to another where you want space you add this image-->"
+ "<tr>"
+ "<td>"
+ "<table id=\"content-6"+"\" cellpadding=\"0"+"\" cellspacing=\"0"+"\" align=\"center"+"\">"
+ "<p align=\"center"+"\">Activate Profile Using the active Code: <b>" + activationCode + "</b>.</p>"
+ "<p align=\"center"+"\"><a href=\"http://www.helloworld.com"+"\">Activate Your Account</a></p>"
+ "</table>"
+ "</td>"
+ "</tr>"
+ "</table><!-- main -->"
+ "<table id=\"bottom-message"+"\" cellpadding=\"20"+"\" cellspacing=\"0"+"\" width=\"600"+"\" align=\"center"+"\">"
+ "<tr>"
+ "<td align=\"center"+"\">"
+ "<p>You are receiving this email because you signed up for updates</p>"
+ "<p><a href=\"http://www.helloworld.com"+"\">Unsubscribe instantly</a> | <a href=\"#"+"\">Forward to a friend</a> | <a href=\"http://www.helloworld.com"+"\">View in Browser</a></p>"
+ "<p> <img src=\"file:///home/yavat6/Downloads/manDoctor.png"+"\" style=\"width:50px;float:left;height:35px;"+"\"/>If you have any queries contact to us via mail 24*7<a href=\"https://www.gmail.com"+"\">CONTACT US</a></p>"
+ "</td>"
+ "</tr>"
+ "</table><!-- bottom message -->"
+ "</table><!-- 100% -->"
+ "</body>"
+ "</html>";
sendMessage(from, password, to,sub, body);
} 发布于 2015-07-26 07:18:20
是的,您可以在java代码中发送HTML文件。您可以使用getResourceAsStream()读取.html文件,然后在String中获取该.html文件的内容,并将该内容作为一条消息发送如下:
InputStream is = Sum.class.getClassLoader().getResourceAsStream("test.html"); // read file as stream, add this html file in yur class path.
BufferedReader br = new BufferedReader(new InputStreamReader(is)) ;
StringBuilder sb = new StringBuilder();
String str = null;
while((str=br.readLine()) != null)
sb.append(str);
String send_to = "test@gmail.com"; // add sender here
Message msg = new MimeMessage(mailSession);
try{
msg.setSubject("Test Email");
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(send_to, false));
String message = sb.toString(); // your html message as string
msg.setContent(message, "text/html; charset=utf-8");
msg.setSentDate(new Date());
Transport.send(msg);
}catch(MessagingException me){
// catch exception here
}https://stackoverflow.com/questions/31634464
复制相似问题