Reputation: 111
I have to send email using javax. The the body of message is html. It has an image tag, My question is what should be the value in the src attribute so that the image is available when the mail is opened in another network.The image is placed inside the server in a folder called images.The application is hosted using apache tomcat 7.
This is the html string which acts as message body.
String emailBody = "<table width=\"99%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"+
"<tr>"+
"<td align=\"center\" valign=\"top\" style = \"background-color:#ED1B24;\">"+
"<img src =\""+
"http://myip:8080/Survey_services" + "/images/mrflogo.jpg" + "\">" +
"</td>" +
"</tr>"+
"<tr>"+
"<td align=\"left\">"+
"<p>Hi "+firstname+"," + "</p></b>"+
"<p>A user account has been created for you, the user name will be your employee id</p></b>"+
"<p>Your Password: "+password+"</p></b>"+
"<p>Kindly change password at your convenience</p></b></b>"+
"<p>Regards,</p></b>"+
"<p>Admin Team</p>"+
"</td>"+
"</tr>"+
"</table>"
I have tried both http://myip:8080/Survey_services and http://localhost:8080/Survey_services , in either case the image is not loaded in the mail when opened in another machine or from Gmail.Rest of the html is fine.
FYI myip is the ipV4 address of the machine where the project is hosted.
EDIT
As per @Bill Shannon suggested I tried the following.
String emailBody = "<table width=\"99%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"+
"<tr>"+
"<td align=\"center\" valign=\"top\" style = \"background-color:#ED1B24;\">"+
"<img src =\""+
"cid:image" + "\">" +
"</td>" +
"</tr>"+
"<tr>"+
"<td align=\"left\">"+
"<p>Hi "+firstname+"," + "</p></b>"+
"<p>A user account has been created for you, the user name will be your employee id</p></b>"+
"<p>Your Password: "+password+"</p></b>"+
"<p>Kindly change password at your convenience</p></b></b>"+
"<p>Regards,</p></b>"+
"<p>Admin Team</p>"+
"</td>"+
"</tr>"+
"</table>";
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
emailMessage.setFrom(new InternetAddress(data[0]));
emailMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(mail));
emailMessage.setSubject("Account created");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(emailBody, "text/html; charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(
"path");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart);
emailMessage.setContent(multipart);
I cant figure out what value I should provide , I tried giving the path of the file in the server by using request.getServletContext().getRealPath("/") ,where request is the request object of the servlet and adding the folder and file name at end. When I do this a an exception is thrown.
Here is the stack trace.
javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.FileNotFoundException: D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Survey_services (Access is denied) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1290) at email.GmailClass.send(GmailClass.java:227) at servlets.CreateUser.doPost(CreateUser.java:168) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.FileNotFoundException: D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Survey_services (Access is denied) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) at javax.activation.FileDataSource.getInputStream(FileDataSource.java:97) at javax.activation.DataHandler.writeTo(DataHandler.java:305) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1645) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:961) at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:553) at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:81) at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:889) at javax.activation.DataHandler.writeTo(DataHandler.java:317) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1645) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1850) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1241) ... 25 more
Any help is appreciated.
Upvotes: 0
Views: 115
Reputation: 29971
Obviously the URL has to correctly point to the image on your server, which you can test by opening the URL in a browser on another network. Even with that, many mail readers won't open images on remote servers by default. The only workaround is to include the image in the message itself by creating a multipart/related message.
Upvotes: 1