From b05dde08eaeb86b547dd9bfd4866852f1a7562d6 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Fri, 25 Mar 2016 20:08:10 +0100 Subject: [PATCH] Spring Emails --- SpringJava/Emails/pom.xml | 220 --------------- SpringJava/Emails/spring-emails-web-client/pom.xml | 125 +++++++++ .../spring/emails/services/EmailMakerService.java | 11 + .../de/spring/emails/services/EmailService.java | 39 +++ .../services/impl/EmailMakerServiceImpl.java | 48 ++++ .../emails/services/impl/EmailServiceImpl.java | 70 +++++ .../rest/controller/EmailController.java | 65 +++++ .../main/resources/email/email-messages.properties | 6 + .../src/main/resources/email/email-template.vm | 17 ++ .../src/main/resources/email/logo.png | Bin 0 -> 3631 bytes .../src/main/resources/email/macro.vm | 3 + .../src/main/resources/log4j2.xml | 42 +++ .../spring-configuration/mvc/rest/rest-config.xml | 88 ++++++ .../spring-configuration/spring-configuration.xml | 64 +++++ .../src/main/webapp/WEB-INF/web.xml | 41 +++ .../spring/emails/services/EmailServiceTest.java | 77 ++++++ SpringJava/Emails/spring-emails/pom.xml | 295 +++++++++++++++++++++ .../emails/services/EmailNotificationService.java | 7 - .../impl/EmailNotificationServiceImpl.java | 54 ---- .../spring-configuration/spring-configuration.xml | 40 --- .../src/main/resources/templates/email-template.vm | 21 -- 21 files changed, 991 insertions(+), 342 deletions(-) delete mode 100644 SpringJava/Emails/pom.xml create mode 100644 SpringJava/Emails/spring-emails-web-client/pom.xml create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/EmailMakerService.java create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/EmailService.java create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerServiceImpl.java create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailServiceImpl.java create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/webservices/rest/controller/EmailController.java create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-messages.properties create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template.vm create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/resources/email/logo.png create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/resources/email/macro.vm create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/resources/log4j2.xml create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/mvc/rest/rest-config.xml create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/spring-configuration.xml create mode 100644 SpringJava/Emails/spring-emails-web-client/src/main/webapp/WEB-INF/web.xml create mode 100644 SpringJava/Emails/spring-emails-web-client/src/test/java/de/spring/emails/services/EmailServiceTest.java create mode 100644 SpringJava/Emails/spring-emails/pom.xml delete mode 100644 SpringJava/Emails/src/main/java/de/spring/emails/services/EmailNotificationService.java delete mode 100644 SpringJava/Emails/src/main/java/de/spring/emails/services/impl/EmailNotificationServiceImpl.java delete mode 100644 SpringJava/Emails/src/main/resources/spring-configuration/spring-configuration.xml delete mode 100644 SpringJava/Emails/src/main/resources/templates/email-template.vm diff --git a/SpringJava/Emails/pom.xml b/SpringJava/Emails/pom.xml deleted file mode 100644 index 0a8ea1d..0000000 --- a/SpringJava/Emails/pom.xml +++ /dev/null @@ -1,220 +0,0 @@ - - 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/spring-emails-web-client/pom.xml b/SpringJava/Emails/spring-emails-web-client/pom.xml new file mode 100644 index 0000000..9162318 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/pom.xml @@ -0,0 +1,125 @@ + + 4.0.0 + + de.spring.emails + spring-emails + 1.0-SNAPSHOT + + + spring-emails-web-client + war + spring-emails-web-client + http://gumartinm.name + Emails with Spring Framework. Web Client. + + Gustavo Martin Morcuende + http://www.gumartinm.name + + + scm:git:http://git.gumartinm.name/JavaForFun + http://git.gumartinm.name/JavaForFun + + + + + + org.springframework + spring-context + + + org.springframework + spring-context-support + + + org.springframework + spring-oxm + + + org.apache.velocity + velocity + + + velocity-tools + velocity-tools + + + javax.mail + mail + + + + org.springframework + spring-webmvc + + + + + javax.servlet + javax.servlet-api + provided + + + + + com.fasterxml.jackson.core + jackson-databind + + + + + + javax.validation + validation-api + + + org.hibernate + hibernate-validator + + + + + + com.icegreen + greenmail + test + + + + ${project.artifactId} + + + ${basedir}/src/main/webapp + + **/*.* + + + + ${basedir}/src/main/resources/ + + **/*.* + + + + + + org.apache.maven.plugins + maven-war-plugin + 2.6 + + + + true + src/main/webapp + + WEB-INF/web.xml + + + + + + + + diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/EmailMakerService.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/EmailMakerService.java new file mode 100644 index 0000000..90f5745 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/EmailMakerService.java @@ -0,0 +1,11 @@ +package de.spring.emails.services; + +import java.util.Locale; +import java.util.Map; + +public interface EmailMakerService { + + public String emailMaker(Map text, String templateLocation, Locale locale); + + public String getSubject(String code, Locale locale, Object... args); +} diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/EmailService.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/EmailService.java new file mode 100644 index 0000000..94cfb8d --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/EmailService.java @@ -0,0 +1,39 @@ +package de.spring.emails.services; + +import java.util.List; +import java.util.Map; + +import javax.mail.MessagingException; + +import org.springframework.core.io.Resource; + +public interface EmailService { + + /** + * This method sends mails. + * + * @param to the email destination. Required parameter. + * @param subject the email subject. Required parameter. + * @param text the email text. Required parameter. + * @param isHtml if true email is HTML type, otherwise false. + * @param attachments file attachments. Optional parameter. + * @param inline inline content in mail. Optional parameter. + * @throws MessagingException in case of any error. + */ + public void sendEmail(String[] to, String subject, String text, boolean isHtml, + List attachments, Map inline) throws MessagingException; + + /** + * This method sends mails. It is asynchronous, what means, this method always returns before sending the email. + * + * @param to the email destination. Required parameter. + * @param subject the email subject. Required parameter. + * @param text the email text. Required parameter. + * @param isHtml if true email is HTML type, otherwise false. + * @param attachments file attachments. Optional parameters. + * @param inline inline content in mail. Optional parameter. + * @throws MessagingException in case of any error. + */ + public void sendEmailAsync(String[] to, String subject, String text, boolean isHtml, + List attachments, Map inline) throws MessagingException; +} \ No newline at end of file diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerServiceImpl.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerServiceImpl.java new file mode 100644 index 0000000..da9f4bf --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailMakerServiceImpl.java @@ -0,0 +1,48 @@ +package de.spring.emails.services.impl; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import org.apache.velocity.app.VelocityEngine; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; +import org.springframework.stereotype.Service; +import org.springframework.ui.velocity.VelocityEngineUtils; + +import de.spring.emails.services.EmailMakerService; + + +@Service("emailMakerService") +public class EmailMakerServiceImpl implements EmailMakerService { + private static final String TEMPLATES_DEFAULT_EXTENSION = ".vm"; + private static final String TEMPLATES_DEFAULT_PATH = "email/"; + private static final String EMAIL_CONTENT_ENCODING = "UTF-8"; + + private final VelocityEngine velocityEngine; + private final MessageSource messageSource; + + @Autowired + public EmailMakerServiceImpl(VelocityEngine velocityEngine, MessageSource messageSource) { + this.velocityEngine = velocityEngine; + this.messageSource = messageSource; + } + + @Override + public String emailMaker(Map text, String templateName, Locale locale) { + final String templateLocation = TEMPLATES_DEFAULT_PATH + templateName + TEMPLATES_DEFAULT_EXTENSION; + final Map model = new HashMap<>(); + model.put("text", text); + model.put("messageSource", messageSource); + model.put("locale", locale); + + return VelocityEngineUtils.mergeTemplateIntoString( + velocityEngine, templateLocation, EMAIL_CONTENT_ENCODING, model); + } + + @Override + public String getSubject(String code, Locale locale, Object... args) { + return messageSource.getMessage(code, args, locale); + } + +} diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailServiceImpl.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailServiceImpl.java new file mode 100644 index 0000000..3ac29c0 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/emails/services/impl/EmailServiceImpl.java @@ -0,0 +1,70 @@ +package de.spring.emails.services.impl; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import de.spring.emails.services.EmailService; + + +@Service("emailService") +public class EmailServiceImpl implements EmailService { + private static final String DEFAULT_FROM_VALUE = "noreply@gumartinm.name"; + + private final JavaMailSender mailSender; + + @Autowired + public EmailServiceImpl(JavaMailSender mailSender) { + this.mailSender = mailSender; + } + + @Override + public void sendEmail(String[] to, String subject, String text, boolean isHtml, + List attachments, Map inline) throws MessagingException { + Assert.notEmpty(to, "required email 'to' field"); + Assert.hasLength(subject, "required email 'subject' field"); + Assert.hasLength(text, "required email 'text' field"); + + final MimeMessage mimeMessage = mailSender.createMimeMessage(); + final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true); + + message.setTo(to); + message.setFrom(DEFAULT_FROM_VALUE); + message.setSubject(subject); + message.setSentDate(new Date()); + message.setText(text, isHtml); + + if (inline != null) { + for (Map.Entry entry : inline.entrySet()) { + message.addInline(entry.getKey(), entry.getValue()); + } + } + + if (attachments != null) { + for (Resource attachment : attachments) { + message.addAttachment(attachment.getFilename(), attachment); + } + } + + + mailSender.send(mimeMessage); + } + + @Override + @Async("asyncEmailSender") + public void sendEmailAsync(String[] to, String subject, String text, boolean isHtml, + List attachments, Map inline) throws MessagingException { + sendEmail(to, subject, text, isHtml, attachments, inline); + } +} diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/webservices/rest/controller/EmailController.java b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/webservices/rest/controller/EmailController.java new file mode 100644 index 0000000..4bcc5b2 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/java/de/spring/webservices/rest/controller/EmailController.java @@ -0,0 +1,65 @@ +package de.spring.webservices.rest.controller; + +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import javax.mail.MessagingException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import de.spring.emails.services.EmailMakerService; +import de.spring.emails.services.EmailService; + +@RestController +@RequestMapping("/api/emails/") +public class EmailController { + private static final Logger LOGGER = LoggerFactory.getLogger(EmailController.class); + private static final String USER = "Gustavo Martin Morcuende"; + private static final String USER_ADDRESS = "noemail@gumartinm.name"; + private static final String TEMPLATE = "email-template"; + private static final String SUBJECT_MESSAGE_KEY = "email.subject"; + + private final EmailService emailService; + private final EmailMakerService emailMakerService; + + @Autowired + public EmailController(EmailService emailService, EmailMakerService emailMakerService) { + this.emailService = emailService; + this.emailMakerService = emailMakerService; + } + + @RequestMapping(method = RequestMethod.GET) + @ResponseStatus(HttpStatus.OK) + public void emails() throws MessagingException { + final String emailSubject = emailMakerService.getSubject(SUBJECT_MESSAGE_KEY, Locale.getDefault()); + final String emailText = doEmailText(); + final String[] to = { USER_ADDRESS }; + final Map inline = new HashMap<>(); + inline.put("cid:mainlogo", new ClassPathResource("email/logo.png")); + try { + emailService.sendEmailAsync(to, emailSubject, emailText, true, null, inline); + } catch (MessagingException ex) { + LOGGER.error("Send email error", ex); + } + } + + private String doEmailText() { + final String isoDateTime = OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); + final Map text = new HashMap<>(); + text.put("user", USER); + text.put("date", isoDateTime); + return emailMakerService.emailMaker(text, TEMPLATE, Locale.getDefault()); + } +} diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-messages.properties b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-messages.properties new file mode 100644 index 0000000..406660d --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-messages.properties @@ -0,0 +1,6 @@ +email.subject = This is an example mail + +email.header1 = New email, just for you. +email.header2 = User: +email.message = New information about something important that you were expecting. + diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template.vm b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template.vm new file mode 100644 index 0000000..a5fc478 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/email-template.vm @@ -0,0 +1,17 @@ + + +

#msg("email.header1")

+

#msg("email.header2")/h2> +

+ ${text.user} +

+

+ #msg("email.message") +

+

+ ${text.date} +

+ + + + \ No newline at end of file diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/logo.png b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..63c8d33bd367c75c8c9a3ce1dd1a4b559fcc1c57 GIT binary patch literal 3631 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 literal 0 HcmV?d00001 diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/macro.vm b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/macro.vm new file mode 100644 index 0000000..6b43382 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/macro.vm @@ -0,0 +1,3 @@ +#macro(msg $code) +$messageSource.getMessage($code, null, $locale) +#end \ No newline at end of file diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/log4j2.xml b/SpringJava/Emails/spring-emails-web-client/src/main/resources/log4j2.xml new file mode 100644 index 0000000..e4e29fa --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/log4j2.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/mvc/rest/rest-config.xml b/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/mvc/rest/rest-config.xml new file mode 100644 index 0000000..54b8540 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/mvc/rest/rest-config.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/spring-configuration.xml b/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/spring-configuration.xml new file mode 100644 index 0000000..2bd1433 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/resources/spring-configuration/spring-configuration.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + class + + org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader + + email/macro.vm + + + + + + + email.email-messages + + + + + + + + + + + + + + diff --git a/SpringJava/Emails/spring-emails-web-client/src/main/webapp/WEB-INF/web.xml b/SpringJava/Emails/spring-emails-web-client/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..1e31c79 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,41 @@ + + + + Spring Email Client: example + + + + org.springframework.web.context.ContextLoaderListener + + + + + spring.profiles.active + ${environment.profile} + contextConfigLocation + + classpath*:spring-configuration/*.xml + + + + + + spring-rest + org.springframework.web.servlet.DispatcherServlet + 1 + true + + contextConfigLocation + classpath*:spring-configuration/mvc/rest/*.xml + + + + + spring-rest + + /* + + + diff --git a/SpringJava/Emails/spring-emails-web-client/src/test/java/de/spring/emails/services/EmailServiceTest.java b/SpringJava/Emails/spring-emails-web-client/src/test/java/de/spring/emails/services/EmailServiceTest.java new file mode 100644 index 0000000..7f93ea0 --- /dev/null +++ b/SpringJava/Emails/spring-emails-web-client/src/test/java/de/spring/emails/services/EmailServiceTest.java @@ -0,0 +1,77 @@ +package de.spring.emails.services; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.internet.MimeMultipart; + +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +import com.icegreen.greenmail.util.GreenMail; +import com.icegreen.greenmail.util.GreenMailUtil; +import com.icegreen.greenmail.util.ServerSetupTest; + +import de.spring.emails.services.impl.EmailServiceImpl; + + +public class EmailServiceTest { + private static final String TO = "noemail@gumartinm.name"; + private static final String SUBJECT = "Email test"; + private static final String TEXT = "Some text in some email"; + + private GreenMail testSmtp; + private EmailService emailService; + + @Before + public void setUp() throws Exception { + JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); + senderImpl.setPort(3025); + senderImpl.setHost("localhost"); + emailService = new EmailServiceImpl(senderImpl); + + testSmtp = new GreenMail(ServerSetupTest.SMTP); + testSmtp.start(); + } + + @After + public void cleanup(){ + testSmtp.stop(); + } + + @Test + public void whenSendEmailWithSuccesThenEmailArrivesToServer() throws MessagingException, IOException { + String[] to = { TO }; + + emailService.sendEmail(to, SUBJECT, TEXT, true, null, null); + + Message[] messages = testSmtp.getReceivedMessages(); + assertEquals(1, messages.length); + assertEquals(SUBJECT, messages[0].getSubject()); + MimeMultipart mp = (MimeMultipart) messages[0].getContent(); + String body = GreenMailUtil.getBody(mp.getBodyPart(0)).trim(); + assertThat(body, CoreMatchers.containsString(TEXT)); + } + + @Test + public void whenSendEmailAsyncWithSuccesThenEmailArrivesToServer() throws MessagingException, IOException { + String[] to = { TO }; + + emailService.sendEmailAsync(to, SUBJECT, TEXT, true, null, null); + + Message[] messages = testSmtp.getReceivedMessages(); + assertEquals(1, messages.length); + assertEquals(SUBJECT, messages[0].getSubject()); + MimeMultipart mp = (MimeMultipart) messages[0].getContent(); + String body = GreenMailUtil.getBody(mp.getBodyPart(0)).trim(); + assertThat(body, CoreMatchers.containsString(TEXT)); + } + +} diff --git a/SpringJava/Emails/spring-emails/pom.xml b/SpringJava/Emails/spring-emails/pom.xml new file mode 100644 index 0000000..63bc9ef --- /dev/null +++ b/SpringJava/Emails/spring-emails/pom.xml @@ -0,0 +1,295 @@ + + 4.0.0 + de.spring.emails + spring-emails + pom + 1.0-SNAPSHOT + spring-emails + http://gumartinm.name + Emails with Spring Framework + + Gustavo Martin Morcuende + http://www.gumartinm.name + + + scm:git:http://git.gumartinm.name/JavaForFun + http://git.gumartinm.name/JavaForFun + + + + 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 + + + + + junit + junit + 4.12 + test + + + org.mockito + mockito-core + 2.0.43-beta + test + + + + + + org.springframework + spring-context + ${spring.version} + + + + commons-logging + commons-logging + + + + + + org.springframework + spring-webmvc + ${spring.version} + + + + commons-logging + commons-logging + + + + + org.springframework + spring-oxm + ${spring.version} + + + + + + javax.servlet + javax.servlet-api + 4.0.0-b01 + provided + + + + + com.fasterxml.jackson.core + jackson-databind + 2.6.4 + + + + + javax.validation + validation-api + 1.1.0.Final + + + org.hibernate + hibernate-validator + 5.2.2.Final + + + + + + + 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 + + + + + + org.springframework + spring-test + ${spring.version} + test + + + + commons-logging + commons-logging + + + + + com.icegreen + greenmail + 1.5.0 + 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.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 deleted file mode 100644 index a7643da..0000000 --- a/SpringJava/Emails/src/main/java/de/spring/emails/services/EmailNotificationService.java +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 9223cd5..0000000 --- a/SpringJava/Emails/src/main/java/de/spring/emails/services/impl/EmailNotificationServiceImpl.java +++ /dev/null @@ -1,54 +0,0 @@ -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 deleted file mode 100644 index fd0aa8e..0000000 --- a/SpringJava/Emails/src/main/resources/spring-configuration/spring-configuration.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - - - 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 deleted file mode 100644 index cd3f34e..0000000 --- a/SpringJava/Emails/src/main/resources/templates/email-template.vm +++ /dev/null @@ -1,21 +0,0 @@ - - -

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