0

In my application user can receive email templates. I'm using velocity engine for sending emails. Email templates are located in the WEB-INF/email_tempaltes/... directory.

Here is my code:

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

Where templateLocation is String variable which obvious looks something like this:

"/email_templates/request-feedback.vm"

Here is how I send mails:

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);
    }

I tested email sending on my local machine and it works fine, but when i deployed it on the server i got an exception:

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

How can I fix it? Where should I store email templates? Thanks in advance!

-----------EDITED:----------- I've created classes directory ander WEB-INF and have added my templates there.

And now I get template like this:

emailSender.sendEmail(mail.getEmail(), "info@somemail.com", "Your message was sent successfully", mail, "/classes/templates/request-sent-answer.vm");

And get the same error:

Could not prepare mail; nested exception is org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 
user3127896
  • 6,323
  • 15
  • 39
  • 65
  • Have you tried that? : http://stackoverflow.com/questions/3443819/velocity-cant-find-resource – alain.janinm Nov 04 '14 at 12:57
  • i've seen that, but i don't understand what should I add resource.loader = class class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader – user3127896 Nov 04 '14 at 13:02
  • In a configuration file you can alos try http://stackoverflow.com/questions/9051413/unable-to-find-velocity-template-resources – alain.janinm Nov 04 '14 at 13:07
  • which configuration file? i don't have any configuration files – user3127896 Nov 04 '14 at 13:23
  • then create one for exemple velocity.properties and put it in web-inf/classes, by the way if you put your template directory in WEB-INF/classes, it will probably work by itself (as suggested in one linked answer) – alain.janinm Nov 04 '14 at 13:35
  • alain.janinm please take a look at the edited part of my question above – user3127896 Nov 04 '14 at 14:18
  • WEB-INF/classes is the default classpath but if you have to create the classes directory yourself it's a pb and it's probably because the classpath is configured elsewhere. So instead of using WEB-INF/classes use the default directory where your compiled classes are stored. – alain.janinm Nov 04 '14 at 14:29
  • solved problem moving email_templates directory to /java/resources foulder – user3127896 Nov 04 '14 at 14:50
  • Ok good. Then write it as an answer and accept it. – alain.janinm Nov 04 '14 at 14:55

1 Answers1

-1

I don't know what mergeTemplateIntoString does, but another solution is to retrieve the template from the classpath and format it using 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);
June
  • 592
  • 8
  • 18