From e5c6656a524f306f26c07fe231021608303eb32e Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Fri, 11 Mar 2016 03:46:12 +0100 Subject: [PATCH] Emails with Spring --- SpringJava/Emails/pom.xml | 220 +++++++++++++++++++++ .../emails/services/EmailNotificationService.java | 7 + .../impl/EmailNotificationServiceImpl.java | 54 +++++ .../spring-configuration/spring-configuration.xml | 40 ++++ .../src/main/resources/templates/email-template.vm | 21 ++ 5 files changed, 342 insertions(+) create mode 100644 SpringJava/Emails/pom.xml create mode 100644 SpringJava/Emails/src/main/java/de/spring/emails/services/EmailNotificationService.java create mode 100644 SpringJava/Emails/src/main/java/de/spring/emails/services/impl/EmailNotificationServiceImpl.java create mode 100644 SpringJava/Emails/src/main/resources/spring-configuration/spring-configuration.xml create mode 100644 SpringJava/Emails/src/main/resources/templates/email-template.vm diff --git a/SpringJava/Emails/pom.xml b/SpringJava/Emails/pom.xml new file mode 100644 index 0000000..0a8ea1d --- /dev/null +++ b/SpringJava/Emails/pom.xml @@ -0,0 +1,220 @@ + + 4.0.0 + de.spring.emails + emails-spring + 1.0-SNAPSHOT + emails-spring + http://gumartinm.name + Sending emails with Spring Framework + + Gustavo Martin Morcuende + http://www.gumartinm.name + + + scm:git:http://git.gumartinm.name/SpringJava/Emails + http://git.gumartinm.name/SpringJava/Emails + + + + UTF-8 + UTF-8 + 4.2.5.RELEASE + + + + + release + + release + + + true + + + + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.3 + + + + org.apache.logging.log4j + log4j-core + 2.3 + + + + org.slf4j + jcl-over-slf4j + 1.7.12 + + + cglib + cglib + 2.2.2 + + + org.springframework + spring-context + ${spring.version} + + + + commons-logging + commons-logging + + + + + + + org.springframework + spring-context-support + ${spring.version} + + + org.apache.velocity + velocity + 1.7 + + + velocity-tools + velocity-tools + 2.0-beta1 + + + + + javax.mail + mail + 1.5.0-b01 + + + javax.activation + activation + 1.1.1 + + + + + junit + junit + 4.12 + test + + + org.springframework + spring-test + ${spring.version} + test + + + + commons-logging + commons-logging + + + + + org.mockito + mockito-core + 2.0.43-beta + test + + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.19.1 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + + ${project.description} + ${project.version} + ${project.organization.name} + ${project.description} + ${project.version} + ${project.organization.name} + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + + integration-test + verify + + + + + + **/*IntegrationTest.java + + + + + + diff --git a/SpringJava/Emails/src/main/java/de/spring/emails/services/EmailNotificationService.java b/SpringJava/Emails/src/main/java/de/spring/emails/services/EmailNotificationService.java new file mode 100644 index 0000000..a7643da --- /dev/null +++ b/SpringJava/Emails/src/main/java/de/spring/emails/services/EmailNotificationService.java @@ -0,0 +1,7 @@ +package de.spring.emails.services; + + +public interface EmailNotificationService { + + public void sendEmail(); +} diff --git a/SpringJava/Emails/src/main/java/de/spring/emails/services/impl/EmailNotificationServiceImpl.java b/SpringJava/Emails/src/main/java/de/spring/emails/services/impl/EmailNotificationServiceImpl.java new file mode 100644 index 0000000..9223cd5 --- /dev/null +++ b/SpringJava/Emails/src/main/java/de/spring/emails/services/impl/EmailNotificationServiceImpl.java @@ -0,0 +1,54 @@ +package de.spring.emails.services.impl; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import org.apache.velocity.app.VelocityEngine; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.mail.javamail.MimeMessagePreparator; +import org.springframework.ui.velocity.VelocityEngineUtils; + +import de.spring.emails.services.EmailNotificationService; + +public class EmailNotificationServiceImpl implements EmailNotificationService { + private final JavaMailSender mailSender; + private final VelocityEngine velocityEngine; + + public EmailNotificationServiceImpl(JavaMailSender mailSender, VelocityEngine velocityEngine) { + this.mailSender = mailSender; + this.velocityEngine = velocityEngine; + } + + @Override + public void sendEmail() { + final MimeMessagePreparator preparator = new MimeMessagePreparator() { + + public void prepare(MimeMessage mimeMessage) throws Exception { + final MimeMessageHelper message = new MimeMessageHelper(mimeMessage); + message.setTo("toemail@example.com"); + message.setBcc("bccemail@example.com"); + message.setFrom(new InternetAddress("fromemail@example.com") ); + message.setSubject("New funny alert"); + + final LocalDateTime dateTime = LocalDateTime.now(); + message.setSentDate(Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant())); + Map model = new HashMap<>(); + model.put("newMessage", ""); + + final String text = VelocityEngineUtils.mergeTemplateIntoString( + velocityEngine, "templates/email-template.vm", "UTF-8", model); + message.setText(text, true); + } + }; + + mailSender.send(preparator); + } + +} diff --git a/SpringJava/Emails/src/main/resources/spring-configuration/spring-configuration.xml b/SpringJava/Emails/src/main/resources/spring-configuration/spring-configuration.xml new file mode 100644 index 0000000..fd0aa8e --- /dev/null +++ b/SpringJava/Emails/src/main/resources/spring-configuration/spring-configuration.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + resource.loader=class + class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader + + + + + + + + + + diff --git a/SpringJava/Emails/src/main/resources/templates/email-template.vm b/SpringJava/Emails/src/main/resources/templates/email-template.vm new file mode 100644 index 0000000..cd3f34e --- /dev/null +++ b/SpringJava/Emails/src/main/resources/templates/email-template.vm @@ -0,0 +1,21 @@ + + +

Dears all

+

+ From - ${newMessage.name} / ${newMessage.email} +

+

+ Something new and exciting. +

+

+ ${newMessage.metadataLine} +

+

+ For further information. +

+

+ ${newMessage.message} +

+ + + \ No newline at end of file -- 2.1.4