I am new to FreeMarker, and i want to use it to send e-mails. My application has integration of Spring 3.1, Hibernate 3.0 and Struts 2 frameworks.
So, basically my code for sending mail is (i'm using java mail api):
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
Address[] addresses = new Address[1];
addresses[0] = new InternetAddress(fromAddress);
message.setReplyTo(addresses);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject(subject);
//To set template using freemarker
BodyPart bodyPart = new MimeBodyPart();
Configuration cfg = new Configuration();
Template template = cfg.getTemplate("template.ftl");
Map<String, String> rootMap = new HashMap<String, String>();
rootMap.put("toName", toName);
rootMap.put("message", sendMessage);
Writer out = new StringWriter();
template.process(rootMap, out);
bodyPart.setContent(out.toString(), "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart);
message.setContent(multipart,"text/html; charset=ISO-8859-1");
Transport.send(message);
But when it tries to send mail,it throws an Exception :
java.io.FileNotFoundException: Template "template.ftl" not found.
The template.ftl
file is in WEB-INF/ftl/
directory.
In my spring-config.xml file, i have added this :
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
</bean>