Announcement Announcement Module
Collapse
No announcement yet.
Spring + JavaMail + Images (relative paths) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + JavaMail + Images (relative paths)

    Hi All,

    I am trying to implement an email notification service with Quartz. To email part, I am using this example 22.3.1.2. Inline resources. So when I pass images in their absolute paths it works perfectly. The problem occurs when I try to use relative paths. Here is a snippet:

    Code:
    private void sendStatusReport(final ReportPack reportPack) {
    
        final MimeMessagePreparator preparator = new MimeMessagePreparator() {
    
    	public void prepare(MimeMessage mimeMessage) throws Exception {
    
    		final MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
    		//...
    		final String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
    				"mail-templates/statusReport.html", getModel(reportPack));
    
    		messageHelper.setText(text, true);
    		loadImages(messageHelper);
    	}
    
    	private void loadImages(MimeMessageHelper messageHelper) throws MessagingException, URISyntaxException, FileNotFoundException {
    		messageHelper.addInline("table_header_left",
    				ResourceUtils.getFile("file:/resources/image/table_header_left.png"));
    		messageHelper.addInline("table_header_right",
    				ResourceUtils.getFile("file:/resources/image/table_header_right.png"));
    		messageHelper.addInline("icon_search",
    				ResourceUtils.getFile("file:/resources/image/icon_search.png"));
    	}
        };
    this.mailSender.send(preparator);
    }
    Here is the exception:

    Code:
    Failed message 1: javax.mail.MessagingException: IOException while sending message;
      nested exception is:
    	java.io.FileNotFoundException: \resources\image\table_header_left.png (The system can not find the file path)
    	at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:422)
    	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:342)
    	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:357)
    	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:346)
    	at br.com.suntech.img.web.email.Mailer.sendStatusReport(Mailer.java:127)
    	at br.com.suntech.img.web.email.Mailer.executeInternal(Mailer.java:69)
    	at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
    	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    	... 1 more
    The problem throws when the email is meant to be sent. I have tried some other approaches like:

    Code:
    1. ResourceUtils.getFile("classpath:resources/image/table_header_left.png"));
    2. ResourceUtils.getFile("classpath*:resources/image/table_header_left.png"));
    3. ResourceUtils.getFile("/resources/image/table_header_left.png"));
    But all fails. I have an enterprise app in this structure:

    Code:
     - app_ear.ear
         - ...
         - app_web.war
             - ...
             - resources
                  - ...
                  - image
                      - ...
                      - table_header_left.png
                      - table_header_right.png
                      - icon_search.png
    The tests are performed with Glassfish. Has someone any clue, please?

    Best Regards,
    Iktuz

  • #2
    Have you tried
    Code:
    ResourceUtils.getURL(classpath:resources/image/table_header_left.png)
    ?

    Comment


    • #3
      Originally posted by travisjwarren View Post
      Have you tried
      Code:
      ResourceUtils.getURL(classpath:resources/image/table_header_left.png)
      ?
      Thanks for replying fellow. I have tried, but it failed:

      Code:
      Caused by: java.io.FileNotFoundException: class path resource [resources/image/table_header_left.png] cannot be resolved to URL because it does not exist
      	at org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:121)
      	at com.app.web.email.Mailer$1.loadImages(Mailer.java:137)
      	at com.app.web.email.Mailer$1.prepare(Mailer.java:96)
      	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:354)
      Here is the code I have used:

      Code:
      //...
      messageHelper.addInline("table_header_left", ResourceUtils.getFile(ResourceUtils.getURL("classpath:resources/image/table_header_left.png").toURI()));
      //...
      Best regards,
      Josť Renato.

      Comment


      • #4
        Chances are you don't need the resourcesin the path.

        Check your project's classpath, the resource location should be relative to that.

        Comment

        Working...
        X