From: Gustavo Martin Morcuende Date: Fri, 25 Mar 2016 19:08:10 +0000 (+0100) Subject: Spring Emails X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=b05dde08eaeb86b547dd9bfd4866852f1a7562d6;p=JavaForFun Spring Emails --- 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 0000000..63c8d33 Binary files /dev/null and b/SpringJava/Emails/spring-emails-web-client/src/main/resources/email/logo.png differ 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