Skip to content

Commit

Permalink
Use velocity templates to build email
Browse files Browse the repository at this point in the history
  • Loading branch information
gortazar committed Mar 28, 2016
1 parent 94d838d commit bf0a11f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package es.urjc.code.dad.mail.batch;

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;

import es.urjc.code.dad.mail.batch.model.Student;

Expand All @@ -18,6 +23,8 @@ public class StudentItemProcessor implements ItemProcessor<Student, MimeMessage>

@Autowired
private JavaMailSender mailSender;
@Autowired
private VelocityEngine engine;
private String sender;
private String attachment;

Expand All @@ -32,10 +39,13 @@ public MimeMessage process(Student student) throws Exception {

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setSubject("Your code");
Map<String, Object> model = new HashMap<>();
model.put("name", student.getFullname());
model.put("code", student.getCode());
helper.setFrom(sender);
helper.setTo(student.getEmail());
helper.setText("This is your code: " + student.getCode() + ".\nFind instructions attached to this email.");
helper.setSubject(VelocityEngineUtils.mergeTemplateIntoString(engine, "email-subject.vm", "UTF-8", model));
helper.setText(VelocityEngineUtils.mergeTemplateIntoString(engine, "email-body.vm", "UTF-8", model));

log.info("Preparing message for: " + student.getEmail());

Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/templates/email-body.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Hi, $name

This is your code: $code

Find instructions attached to this email.

Regards.
1 change: 1 addition & 0 deletions src/main/resources/templates/email-subject.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Your code

0 comments on commit bf0a11f

Please sign in to comment.