Results 1 to 4 of 4

Thread: Spring + JavaMail + Images (relative paths)

  1. #1
    Join Date
    Feb 2010
    Posts
    6

    Default 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. #2
    Join Date
    Aug 2007
    Location
    Brisbane, Australia
    Posts
    33

    Default

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

  3. #3
    Join Date
    Feb 2010
    Posts
    6

    Default

    Quote 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.

  4. #4
    Join Date
    Aug 2007
    Location
    Brisbane, Australia
    Posts
    33

    Default

    Chances are you don't need the resourcesin the path.

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •