Template HTML file to send with JavaMail
Hi,
I have a HTML file that i have to send with the Javamail api from an struts Action. I have 2 doubts:
1. How can i include in the HTML file any "TEXT TAG" to insert in it
automatically variable values (TEXT variables)? (Remember that I
am sending the message from a Struts Action)
2. To send the message, now I am openning the file, reading it all,
creating a StringBuffer and adding it to the:
messageBodyPart.setContent(htmlText, "text/html");
Could I say to the api "take this whole file and send it"??
This is my template HTML (now. I would want a complete template)
<html>
<body topmargin="0" leftmargin="3" rightmargin="3">
<img src="cid:memememe">
<table width="85%" border=1 heigth="80%">
<tr>
<td> </td>
<td width="30%">
<table border=1>
This is my source code
String text="";
String aux = null;
try{
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("/html/jsp/public/TemplateEmail.html")));
while((aux = dis.readLine()) != null)
text += aux;
dis.close();
}catch(Exception e){
e.printStackTrace();
}
String htmlText=text;
htmlText += "<tr><td align=left width=20%><strong>NIF:</strong></td><td>"+MyObject.getNifUsu()+"</td></tr>";
htmlText += "<tr><td align=left width=20%><strong>Nombre:</strong></td><td>"+MyObject.getNomUsu()+"</td></tr>";
htmlText += "<tr><td align=left width=20%><strong>Apellidos:</strong></td><td>"+MyObject.getApelUsu()+"</td></tr>";
htmlText += "</table></td><td align=center><img src=\"cid:memememe2\"></td>"+
"</tr></table>" +
"</body></html>";
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(htmlText, "text/html");
//Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);
//HERE I ATTACHT A IMAGE
messageBodyPart = new MimeBodyPart();
//Fetch the image and associate to part
String file = servlet.getServletContext().getRealPath("/images/MyImage.JPG");
DataSource fds = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","memememe");
//Add part to multi-part
multipart.addBodyPart(messageBodyPart);