PDA

View Full Version : yet another velocity email problem


lumpynose
Apr 24th, 2006, 08:12 PM
Unlike lime I'm trying to use a template that's a file on disk; no database stuff for me with velocity. When it gets to the point to load the template it's always looking in whatever directory I was in when I start tomcat, if I don't use an absolute path. If I use an absolute path then it's starting from the root of the filesystem, not from WEB-INF or my classpath.

Can someone please tell me what I'm doing wrong?

public class
EmailSender {
private final static Log log = LogFactory.getLog(EmailSender.class);

private final static String from = "velocity@it-aint-working.com";
private final static String subj = "My very first Velocity message";

private JavaMailSenderImpl mailSender;
private Template template;
private DateFormat dateFormat;
private ServletContextResource msgTemplate;

public void
send(LoginUserInfo user) {
SimpleMailMessage msg = new SimpleMailMessage();

msg.setFrom(from);
msg.setReplyTo(from);
msg.setSubject(subj);
msg.setTo(user.getEmail());
msg.setText(messageText(user));

mailSender.setHost("localhost");
mailSender.send(msg);
}

private String
messageText(LoginUserInfo user) {
Date now = new Date();

VelocityContext context = new VelocityContext();

context.put("date", dateFormat.format(now));
context.put("host", user.getClientHost());

StringWriter sw = new StringWriter();

try {
template.merge(context, sw);
}
catch (ResourceNotFoundException e) {
throw(new MailPreparationException(e));
}
catch (ParseErrorException e) {
throw(new MailPreparationException(e));
}
catch (MethodInvocationException e) {
throw(new MailPreparationException(e));
}
catch (Exception e) {
throw(new MailPreparationException(e));
}

return(sw.toString());
}

public void
init() {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL);

VelocityEngine velocityEngine = new VelocityEngine();

try {
velocityEngine.init();
}
catch (Exception e) {
throw(new MailPreparationException(e));
}

try {
template = velocityEngine.getTemplate("email.vm");
}
catch (ResourceNotFoundException e) {
throw(new MailPreparationException(e));
}
catch (ParseErrorException e) {
throw(new MailPreparationException(e));
}
catch (Exception e) {
throw(new MailPreparationException(e));
}
}
...

lumpynose
Apr 24th, 2006, 08:58 PM
Nevermind, I figured it out from a post on Matt Raible (%6d%61%74%74%40%72%61%69%62%6c%65%64%65%73%69%67% 6e%73%2e%63%6f%6d)'s blog.