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