Cant retrieve an images from the WEB-INF folder ussing classLoader.getResourceAsStream()

The hold noon i was trying to make my app send both html+images via javamail, i only managed to send html, but with the image i am having a bit of problems. I decided to create a multipart message, and all was going ok, but then i use the class loader to retrieve the .png file from the WEB-INF/resources/images i get a NullPointerExcetion, i dont know why is that? Here is how my EJB(3.0) looks like. Ill appreciate a hand on this one i dont have much experience with the ClassLoader class(Dont know much about it). @Stateless(name = "ejbs/EmailServiceEJB") public class EmailServiceEJB implements IEmailServiceEJB { @Resource(name = "mail/myMailSession") private Session mailSession; public void sendAccountActivationLinkToBuyer(String destinationEmail, String name) { // Destination of the email String to = destinationEmail; String from = "dontreply2thismessage@gmail.com"; try { Message message = new MimeMessage(mailSession); // From: is our service message.setFrom(new InternetAddress(from)); // To: destination given message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject("Uspijesna registracija"); // How to found at http://www.rgagnon.com/javadetails/java-0321.html message.setContent(generateActivationLinkTemplate(), "text/html"); Date timeStamp = new Date(); message.setSentDate(timeStamp); // Prepare a multipart HTML Multipart multipart = new MimeMultipart(); // Prepare the HTML BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(generateActivationLinkTemplate(), "text/html"); multipart.addBodyPart(htmlPart); // PREPARE THE IMAGE BodyPart imgPart = new MimeBodyPart(); String fileName = "/WEB-INF/resources/images/logoemailtemplate.png"; ClassLoader classLoader = Thread.currentThread() .getContextClassLoader(); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); if (classLoader == null) { System.out.println("IT IS NULL AGAIN!!!!"); } } DataSource ds = new URLDataSource(classLoader.getResource(fileName)); imgPart.setDataHandler(new DataHandler(ds)); imgPart.setHeader("Content-ID", "the-img-1"); multipart.addBodyPart(imgPart); // Set the message content! message.setContent(multipart); Transport.send(message); } catch (MessagingException e) { throw new RuntimeException(e); } } I would like to mention that i am ussing JEE6 with glassfishV3 i dont know if my approach is compatible with this application server. ----------------------------------------------------------------------------------------- Update When i modify the above code to String fileName = "logoemailtemplate.png"; I receive an email, it works. But now I don't receive the text. :) Is there any mistake?

以上就是Cant retrieve an images from the WEB-INF folder ussing classLoader.getResourceAsStream()的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏