2014-11-04 83 views
0

在我的應用程序中,用戶可以接收電子郵件模板。我正在使用velocity engine發送電子郵件。電子郵件模板位於WEB-INF/email_tempaltes/...目錄中。速度引擎無法找到資源

這裏是我的代碼:

String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateLocation, "UTF-8", model); 

哪裏templateLocationString variable這明顯看起來是這樣的:

"/email_templates/request-feedback.vm" 

這裏是我發送郵件:

private void sendEmail(final String toEmailAddresses, final String fromEmailAddress, 
          final String subject, final String attachmentPath, 
          final String attachmentName, final Mail mail, final String templateLocation) { 
     Properties properties = new Properties(); 
     MimeMessagePreparator preparator = new MimeMessagePreparator() { 
      public void prepare(MimeMessage mimeMessage) throws Exception { 
       MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); 
       message.setTo(toEmailAddresses); 
       message.setFrom(new InternetAddress(fromEmailAddress)); 
       message.setSubject(subject); 
       Map<String, Object> model = new HashMap<String, Object>(); 
       model.put("phone", mail.getPhone()); 
       model.put("email", mail.getEmail()); 
       model.put("message", mail.getMessage()); 
       String body = VelocityEngineUtils.mergeTemplateIntoString(
         velocityEngine, templateLocation, "UTF-8", model); 
       message.setText(body, true); 
       if (!StringUtils.isBlank(attachmentPath)) { 
        FileSystemResource file = new FileSystemResource(attachmentPath); 
        message.addAttachment(attachmentName, file); 
       } 
      } 
     }; 
     this.mailSender.send(preparator); 
    } 

我測試電子郵件發送在我的本地機器上,它工作正常,但是當id在服務器上展示它我得到一個異常:

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource /email_templates/request-feedback.vm' 

我該如何解決它?我應該在哪裏存儲郵件模板?提前致謝!

-----------編輯:----------- 我創建了類目錄ander WEB-INF,並在那裏添加了我的模板。

現在我得到的模板是這樣的:

emailSender.sendEmail(mail.getEmail(), "[email protected]", "Your message was sent successfully", mail, "/classes/templates/request-sent-answer.vm"); 

並得到了同樣的錯誤:

Could not prepare mail; nested exception is org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 
+0

你試過嗎? :http://stackoverflow.com/questions/3443819/velocity-cant-find-resource – 2014-11-04 12:57:46

+0

我見過,但我不明白我應該添加什麼resource.loader =類 class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader – user3127896 2014-11-04 13:02:54

+0

在配置文件中,你可以嘗試http://stackoverflow.com/questions/9051413/unable-to-find-velocity-template-resources – 2014-11-04 13:07:15

回答

-1

我不知道mergeTemplateIntoString做什麼,但另一種解決方案是檢索從模板類路徑並使用MessageFormat格式化。

String result; 
final StringWriter templateData = new StringWriter(); 
final InputStream templateIn = YourClassName.class.getClassLoader().getResourceAsStream(path); 
    for(int b=templateIn.read(); b != -1; b=templateIn.read()){ 
    templateData.write(b); 
    } 
templateIn.close(); 
result = MessageFormat.format(templateData.toString(), arguments);