-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b80160c
Showing
12 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/target/ | ||
/mvnw | ||
/mvnw.cmd | ||
.classpath | ||
.project | ||
.mvn | ||
.settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
# Mail batch application | ||
|
||
A sample Spring Batch application to send emails in bulk. It feeds email addresses from a csv file and sends an email to each recipient listed in the file with a file attached. A sample csv file and attachment file are provided as samples. | ||
|
||
## How to run the application | ||
|
||
Java 8 is required. To run the application: | ||
|
||
mvn clean package | ||
java -jar target/mail-batch-0.0.1-SNAPSHOT.jar \ | ||
--spring.mail.host=<host> \ | ||
--spring.mail.port=<port> \ | ||
--spring.mail.username=<user> \ | ||
--spring.mail.password=<pass> \ | ||
--codeurjc.batch.data=data.csv \ | ||
--codeurjc.batch.attachment=sample-attachment.txt | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"Full name","Code","Email" | ||
"Pete Peterson","M500","[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>es.urjc.code.dad</groupId> | ||
<artifactId>mail-batch</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>mail-batch</name> | ||
<description>Send mails with Spring Batch project</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.3.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-batch</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-mail</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.scala-lang</groupId> | ||
<artifactId>scala-library</artifactId> | ||
<version>2.11.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
You should've received this file attached to your mail |
102 changes: 102 additions & 0 deletions
102
src/main/java/es/urjc/code/dad/mail/batch/BatchConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package es.urjc.code.dad.mail.batch; | ||
|
||
import javax.mail.internet.MimeMessage; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobExecutionListener; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.core.launch.support.RunIdIncrementer; | ||
import org.springframework.batch.item.file.FlatFileItemReader; | ||
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; | ||
import org.springframework.batch.item.file.mapping.DefaultLineMapper; | ||
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.FileSystemResource; | ||
|
||
import es.urjc.code.dad.mail.batch.model.Student; | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
public class BatchConfiguration { | ||
|
||
@Autowired | ||
public JobBuilderFactory jobBuilderFactory; | ||
|
||
@Autowired | ||
public StepBuilderFactory stepBuilderFactory; | ||
|
||
@Value("${spring.mail.username}") | ||
private String sender; | ||
|
||
@Value("${codeurjc.batch.data}") | ||
public String data; | ||
|
||
@Value("${codeurjc.batch.attachment}") | ||
private String attachment; | ||
|
||
// tag::readerwriterprocessor[] | ||
@Bean | ||
public FlatFileItemReader<Student> reader() { | ||
FlatFileItemReader<Student> reader = new FlatFileItemReader<>(); | ||
reader.setResource(new FileSystemResource(data)); | ||
reader.setLinesToSkip(1); | ||
reader.setLineMapper(new DefaultLineMapper<Student>() {{ | ||
setLineTokenizer(new DelimitedLineTokenizer() {{ | ||
setNames(new String[] {"fullname", "code", "email"} ); | ||
}}); | ||
setFieldSetMapper(new BeanWrapperFieldSetMapper<Student>(){{ | ||
setTargetType(Student.class); | ||
}}); | ||
}}); | ||
return reader; | ||
} | ||
|
||
@Bean | ||
public StudentItemProcessor processor() { | ||
return new StudentItemProcessor(sender, attachment); | ||
} | ||
|
||
@Bean | ||
public MailBatchItemWriter writer() { | ||
MailBatchItemWriter writer = new MailBatchItemWriter(); | ||
return writer; | ||
} | ||
// end::readerwriterprocessor[] | ||
|
||
// tag::listener[] | ||
|
||
@Bean | ||
public JobExecutionListener listener() { | ||
return new JobCompletionNotificationListener(); | ||
} | ||
|
||
// end::listener[] | ||
|
||
// tag::jobstep[] | ||
@Bean | ||
public Job importUserJob() { | ||
return jobBuilderFactory.get("importUserJob") | ||
.incrementer(new RunIdIncrementer()) | ||
.listener(listener()) | ||
.flow(step1()) | ||
.end() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step step1() { | ||
return stepBuilderFactory.get("step1") | ||
.<Student, MimeMessage> chunk(10) | ||
.reader(reader()) | ||
.processor(processor()) | ||
.writer(writer()) | ||
.build(); | ||
} | ||
// end::jobstep[] | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/es/urjc/code/dad/mail/batch/JobCompletionNotificationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package es.urjc.code.dad.mail.batch; | ||
|
||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.listener.JobExecutionListenerSupport; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
public class JobCompletionNotificationListener extends JobExecutionListenerSupport { | ||
|
||
@Autowired | ||
private JavaMailSender mailSender; | ||
|
||
@Override | ||
public void afterJob(JobExecution jobExecution) { | ||
|
||
SimpleMailMessage message = new SimpleMailMessage(); | ||
|
||
message.setSubject("Job completion"); | ||
message.setFrom("[email protected]"); | ||
message.setTo("[email protected]"); | ||
message.setText("Job completed with " + jobExecution.getExitStatus()); | ||
|
||
mailSender.send(message); | ||
} | ||
|
||
@Override | ||
public void beforeJob(JobExecution jobExecution) { | ||
|
||
SimpleMailMessage message = new SimpleMailMessage(); | ||
|
||
message.setSubject("Job started"); | ||
message.setFrom("[email protected]"); | ||
message.setTo("[email protected]"); | ||
message.setText("Job started"); | ||
|
||
mailSender.send(message); | ||
|
||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/es/urjc/code/dad/mail/batch/MailBatchApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package es.urjc.code.dad.mail.batch; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class MailBatchApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MailBatchApplication.class, args); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/es/urjc/code/dad/mail/batch/MailBatchItemWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package es.urjc.code.dad.mail.batch; | ||
|
||
import java.util.List; | ||
|
||
import javax.mail.internet.MimeMessage; | ||
|
||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
public class MailBatchItemWriter implements ItemWriter<MimeMessage> { | ||
|
||
@Autowired | ||
private JavaMailSender mailSender; | ||
|
||
@Override | ||
public void write(List<? extends MimeMessage> messages) throws Exception { | ||
|
||
messages.stream().forEach((message)->mailSender.send(message)); | ||
|
||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/es/urjc/code/dad/mail/batch/StudentItemProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package es.urjc.code.dad.mail.batch; | ||
|
||
import javax.mail.internet.MimeMessage; | ||
|
||
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 es.urjc.code.dad.mail.batch.model.Student; | ||
|
||
public class StudentItemProcessor implements ItemProcessor<Student, MimeMessage> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(StudentItemProcessor.class); | ||
|
||
@Autowired | ||
private JavaMailSender mailSender; | ||
private String sender; | ||
private String attachment; | ||
|
||
public StudentItemProcessor(String sender, String attachment) { | ||
this.sender = sender; | ||
this.attachment = attachment; | ||
} | ||
|
||
@Override | ||
public MimeMessage process(Student student) throws Exception { | ||
MimeMessage message = mailSender.createMimeMessage(); | ||
|
||
MimeMessageHelper helper = new MimeMessageHelper(message, true); | ||
|
||
helper.setSubject("Your code"); | ||
helper.setFrom(sender); | ||
helper.setTo(student.getEmail()); | ||
helper.setText("This is your code: " + student.getCode() + ".\nFind instructions attached to this email."); | ||
|
||
log.info("Preparing message for: " + student.getEmail()); | ||
|
||
FileSystemResource file = new FileSystemResource(attachment); | ||
helper.addAttachment(file.getFilename(), file); | ||
|
||
return message; | ||
} | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/es/urjc/code/dad/mail/batch/model/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package es.urjc.code.dad.mail.batch.model; | ||
|
||
public class Student { | ||
|
||
private String fullname; | ||
private String code; | ||
private String email; | ||
|
||
public Student() { | ||
} | ||
|
||
public String getFullname() { | ||
return fullname; | ||
} | ||
|
||
public void setFullname(String fullname) { | ||
this.fullname = fullname; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#springboot-starter-mail properties | ||
#spring.mail.host= | ||
#spring.mail.port= | ||
#spring.mail.username= | ||
#spring.mail.password= | ||
spring.mail.test-connection=true | ||
|
||
# Change this according to your mail server if necessary | ||
spring.mail.properties.mail.smtp.auth=true | ||
spring.mail.properties.mail.transport.protocol=tls | ||
spring.mail.properties.mail.smtp.starttls.enable=true | ||
|
||
# Path to csv data file (first line will be skipped) | ||
#codeurjc.batch.data= | ||
# Path to attachment data (this file will be attached to all emails sent) | ||
#codeurjc.batch.attachment= |