From 24c25206a4d8b748e727f0cc9db5d64d456017e8 Mon Sep 17 00:00:00 2001 From: oreva Date: Thu, 28 Jul 2016 18:41:17 +0300 Subject: [PATCH 01/13] Spring MVC Email application added. --- spring-mvc-email/pom.xml | 142 ++++++++++++++++++ .../com/baeldung/spring/config/AppConfig.java | 100 ++++++++++++ .../spring/controllers/HomeController.java | 19 +++ .../spring/controllers/MailController.java | 44 ++++++ .../com/baeldung/spring/mail/Application.java | 20 +++ .../com/baeldung/spring/mail/MailService.java | 73 +++++++++ .../baeldung/spring/web/dto/MailObject.java | 50 ++++++ .../main/resources/META-INF/application.xml | 19 +++ .../src/main/webapp/WEB-INF/views/home.jsp | 25 +++ .../main/webapp/WEB-INF/views/mail/send.jsp | 60 ++++++++ .../src/main/webapp/WEB-INF/web.xml | 28 ++++ 11 files changed, 580 insertions(+) create mode 100644 spring-mvc-email/pom.xml create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/Application.java create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/MailService.java create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java create mode 100644 spring-mvc-email/src/main/resources/META-INF/application.xml create mode 100644 spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp create mode 100644 spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp create mode 100644 spring-mvc-email/src/main/webapp/WEB-INF/web.xml diff --git a/spring-mvc-email/pom.xml b/spring-mvc-email/pom.xml new file mode 100644 index 0000000000..0ac84f5cc9 --- /dev/null +++ b/spring-mvc-email/pom.xml @@ -0,0 +1,142 @@ + + + 4.0.0 + + org.baeldung.spring + SpringMVCEmail + war + 1.0 + + + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + + javax.servlet + jstl + 1.2 + + + + + org.springframework + spring-context + 4.2.5.RELEASE + + + + org.springframework + spring-core + 4.2.5.RELEASE + + + + org.springframework + spring-web + 4.2.5.RELEASE + + + + org.springframework + spring-webmvc + 4.2.5.RELEASE + + + + + javax.validation + validation-api + 1.1.0.Final + + + + + org.hibernate + hibernate-validator + 5.0.1.Final + + + + + org.springframework.data + spring-data-jpa + 1.10.1.RELEASE + + + + + org.springframework.security + spring-security-web + 4.0.4.RELEASE + + + org.springframework.security + spring-security-config + 4.0.4.RELEASE + + + + + com.sun.mail + javax.mail + 1.5.5 + + + + + org.springframework.integration + spring-integration-mail + 4.3.0.RELEASE + + + + + SpringMVCEmail + + + + + org.apache.tomcat.maven + tomcat6-maven-plugin + 2.2 + + http://localhost:8080/manager/text + TomcatServer + /SpringMVCEmail + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-war-plugin + 2.4 + + src/main/webapp/WEB-INF/web.xml + + + src/main/resources/META-INF + + + + + + + \ No newline at end of file diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java b/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java new file mode 100644 index 0000000000..2a7caadf23 --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java @@ -0,0 +1,100 @@ +package com.baeldung.spring.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.web.servlet.config.annotation.*; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; +import org.springframework.web.servlet.view.UrlBasedViewResolver; + +import java.util.Properties; + +/** + * Created with IntelliJ IDEA. + * User: Olga + */ +@Configuration +@ComponentScan("com.baeldung.spring") +@EnableWebMvc //tha same as +public class AppConfig extends WebMvcConfigurerAdapter { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Bean + public UrlBasedViewResolver urlBasedViewResolver() { + UrlBasedViewResolver resolver = new UrlBasedViewResolver(); + resolver.setOrder(0); + resolver.setPrefix("/WEB-INF/views/"); + resolver.setSuffix(".jsp"); + resolver.setCache(false); + resolver.setViewClass(JstlView.class); + return resolver; + } + + @Bean + public InternalResourceViewResolver internalResourceViewResolver() { + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setOrder(1); + resolver.setPrefix("/WEB-INF/views/"); + resolver.setSuffix(".jsp"); + resolver.setViewClass(JstlView.class); + return resolver; + } + + /* Gmail */ + @Bean + public JavaMailSenderImpl mailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost("smtp.gmail.com"); + mailSender.setPort(587); + mailSender.setUsername("reva.olga"); + mailSender.setPassword("zakpnejnnoqkehgs"); + mailSender.setJavaMailProperties(javaMailProperties()); + return mailSender; + } + + private Properties javaMailProperties() { + Properties props = new Properties(); + props.setProperty("mail.smtp.auth", "true"); + props.setProperty("mail.smtp.starttls.enable", "true"); + return props; + } + + @Bean + public SimpleMailMessage templateMessage() { + SimpleMailMessage message = new SimpleMailMessage(); + message.setText("This is automatically generated email,\n" + + "Original mail text is included : %s\n" + + "\n" + + "Sincerely yours,\n" + + "Yourcompany."); + return message; + } + + /*Amazon SES + @Bean + public JavaMailSenderImpl mailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost("email-smtp.us-west-2.amazonaws.com"); + mailSender.setUsername("username"); + mailSender.setPassword("password"); + mailSender.setJavaMailProperties(javaMailProperties()); + return mailSender; + } + + private Properties javaMailProperties() { + Properties props = new Properties(); + props.setProperty("mail.transport.protocol", "smtps"); + props.setProperty("mail.smtp.port", "25"); + + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + props.put("mail.smtp.starttls.required", "true"); + return props; + }*/ +} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java new file mode 100644 index 0000000000..656e237a9e --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Created with IntelliJ IDEA. + * User: Olga + */ +@Controller +@RequestMapping({"/","/home"}) +public class HomeController { + + @RequestMapping(method = RequestMethod.GET) + public String showHomePage() { + return "home"; + } +} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java new file mode 100644 index 0000000000..5de722f444 --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java @@ -0,0 +1,44 @@ +package com.baeldung.spring.controllers; + +import com.baeldung.spring.mail.MailService; +import com.baeldung.spring.web.dto.MailObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.Errors; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import javax.validation.Valid; + +/** + * Created by Olga on 7/20/2016. + */ +@Controller +@RequestMapping("/mail") +public class MailController { + @Autowired + public MailService mailService; + + @RequestMapping(value = "/send", method = RequestMethod.GET) + public String createMail(Model model) { + model.addAttribute("mailObject", new MailObject()); + return "mail/send"; + } + + @RequestMapping(value = "/send", method = RequestMethod.POST) + public String createMail(Model model, + @ModelAttribute("mailObject") @Valid MailObject mailObject, + Errors errors) { + if(errors.hasErrors()) { + return "mail/send"; + } + + SimpleMailMessage mailMessage = mailService.createSimpleMailMessage(mailObject); + mailService.sendMail(mailMessage); + + return "redirect:/home"; + } +} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Application.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Application.java new file mode 100644 index 0000000000..68f89f778d --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Application.java @@ -0,0 +1,20 @@ +package com.baeldung.spring.mail; + +import org.springframework.mail.SimpleMailMessage; + +/** + * Created by Olga on 7/15/2016. + */ +public class Application { + + public static void main(String ...args) { + MailService mailService = new MailService(); + + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom("from@test.com"); + message.setTo("to@test.com"); + message.setSubject("Test Message"); + + mailService.sendMail(message); + } +} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/MailService.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/MailService.java new file mode 100644 index 0000000000..4deab12e0a --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/MailService.java @@ -0,0 +1,73 @@ +package com.baeldung.spring.mail; + +import com.baeldung.spring.web.dto.MailObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.MailException; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.io.File; + +/** + * Created by Olga on 7/15/2016. + */ +@Service +public class MailService { + @Autowired + public JavaMailSender mailSender; + + public void sendMail(MimeMessage message) { + try { + mailSender.send(message); + } catch (MailException exception) { + exception.printStackTrace(); + } + } + + public MimeMessage createMessageWithAttachment(MailObject mailObject) { + MimeMessage message = mailSender.createMimeMessage(); + try { + // pass 'true' to the constructor to create a multipart message + MimeMessageHelper helper = new MimeMessageHelper(message, true); + + helper.setFrom(mailObject.getFrom()); + helper.setTo(mailObject.getTo()); + helper.setSubject(mailObject.getSubject()); + helper.setText(mailObject.getText()); + + // attach a sample image attachment + FileSystemResource file = new FileSystemResource(new File("c:/attachment.jpg")); + helper.addAttachment("Attachment.jpg", file); + } catch (MessagingException e) { + e.printStackTrace(); + } + return message; + } + + public void sendMail(SimpleMailMessage message) { + try { + mailSender.send(message); + } catch (MailException exception) { + exception.printStackTrace(); + } + } + + @Autowired + public SimpleMailMessage template; + + public SimpleMailMessage createSimpleMailMessage(MailObject mailObject) { + SimpleMailMessage mailMessage = new SimpleMailMessage(template); + + mailMessage.setFrom(mailObject.getFrom()); + mailMessage.setTo(mailObject.getTo()); + mailMessage.setSubject(mailObject.getSubject()); + mailMessage.setText(String.format(template.getText(), mailObject.getText())); + + return mailMessage; + } +} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java b/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java new file mode 100644 index 0000000000..70975be84c --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java @@ -0,0 +1,50 @@ +package com.baeldung.spring.web.dto; + +import org.hibernate.validator.constraints.Email; + +import javax.validation.constraints.NotNull; + +/** + * Created by Olga on 7/20/2016. + */ +public class MailObject { + @Email + private String from; + @NotNull + @Email + private String to; + private String subject; + private String text; + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/spring-mvc-email/src/main/resources/META-INF/application.xml b/spring-mvc-email/src/main/resources/META-INF/application.xml new file mode 100644 index 0000000000..f2d901b497 --- /dev/null +++ b/spring-mvc-email/src/main/resources/META-INF/application.xml @@ -0,0 +1,19 @@ + + + + + + SpringMVCEmail.war + SpringMVCEmail + + + + + web.war + SpringMVCEmail + + + \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp b/spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp new file mode 100644 index 0000000000..74165f2626 --- /dev/null +++ b/spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp @@ -0,0 +1,25 @@ +<%-- + Created by IntelliJ IDEA. + User: Olga + Date: 1/19/16 + Time: 3:53 PM + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + Home Page + + +
+
+

Send Email

+
+ +
+
+
+ + \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp new file mode 100644 index 0000000000..9e9cbc1ff7 --- /dev/null +++ b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp @@ -0,0 +1,60 @@ +<%-- + Created by IntelliJ IDEA. + User: Olga + Date: 7/20/2016 + Time: 1:47 PM + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + Send Email + + +
+

Create Email

+ +
+ + + + + + + + + + + + + + + + + + + + +
+ Enter email address
+ +
+ Enter your email address
+ +
+ Enter the subject
+ +
+ Enter message text
+ +
+ Send +
+
+
+
+ + diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..5fe46d45d6 --- /dev/null +++ b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,28 @@ + + + + + simpleweb + org.springframework.web.servlet.DispatcherServlet + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + com.baeldung.spring.config.AppConfig + + 1 + + + + simpleweb + / + + From 2bef18b3db5c1362f5ac7f9a3574762cba9c13ca Mon Sep 17 00:00:00 2001 From: oreva Date: Thu, 28 Jul 2016 21:56:36 +0300 Subject: [PATCH 02/13] Validation for empty "to" and "from" fields added. --- .../main/java/com/baeldung/spring/web/dto/MailObject.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java b/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java index 70975be84c..3369ccd9f6 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java @@ -3,15 +3,19 @@ package com.baeldung.spring.web.dto; import org.hibernate.validator.constraints.Email; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; /** * Created by Olga on 7/20/2016. */ public class MailObject { @Email - private String from; @NotNull + @Size(min = 1, message = "Please, specify your email address") + private String from; @Email + @NotNull + @Size(min = 1, message = "Please, set an email address to send the message to it") private String to; private String subject; private String text; From 8c21d6a3b090cc9c89ee034e8b1fccb22bd742d0 Mon Sep 17 00:00:00 2001 From: oreva Date: Thu, 28 Jul 2016 21:57:56 +0300 Subject: [PATCH 03/13] My personal username and pwd removed, test strings added instead. --- .../src/main/java/com/baeldung/spring/config/AppConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java b/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java index 2a7caadf23..4c5286490b 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java @@ -52,8 +52,8 @@ public class AppConfig extends WebMvcConfigurerAdapter { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.gmail.com"); mailSender.setPort(587); - mailSender.setUsername("reva.olga"); - mailSender.setPassword("zakpnejnnoqkehgs"); + mailSender.setUsername("username"); + mailSender.setPassword("password"); mailSender.setJavaMailProperties(javaMailProperties()); return mailSender; } From 468ccfc2168c004c9e9a5458ea353df7b487ab00 Mon Sep 17 00:00:00 2001 From: oreva Date: Thu, 18 Aug 2016 22:33:41 +0300 Subject: [PATCH 04/13] Spring-boot-starter-mail added. Don't work (JavaMailSender is not created automatically). --- spring-mvc-email/pom.xml | 160 +++++++----------- spring-mvc-email/pom_old.xml | 142 ++++++++++++++++ .../java/com/baeldung/spring/Application.java | 35 ++++ .../spring/{ => app}/config/AppConfig.java | 17 +- .../baeldung/spring/application.properties | 4 + .../spring/controllers/HomeController.java | 8 +- .../spring/controllers/MailController.java | 16 +- .../com/baeldung/spring/mail/Application.java | 20 --- ...MailService.java => EmailServiceImpl.java} | 26 +-- .../main/resources/META-INF/application.xml | 2 +- .../main/webapp/WEB-INF/simpleweb-servlet.xml | 23 +++ .../src/main/webapp/WEB-INF/web.xml | 19 ++- 12 files changed, 307 insertions(+), 165 deletions(-) create mode 100644 spring-mvc-email/pom_old.xml create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/Application.java rename spring-mvc-email/src/main/java/com/baeldung/spring/{ => app}/config/AppConfig.java (89%) create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/application.properties delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/Application.java rename spring-mvc-email/src/main/java/com/baeldung/spring/mail/{MailService.java => EmailServiceImpl.java} (81%) create mode 100644 spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml diff --git a/spring-mvc-email/pom.xml b/spring-mvc-email/pom.xml index 0ac84f5cc9..6b2ef0bf9b 100644 --- a/spring-mvc-email/pom.xml +++ b/spring-mvc-email/pom.xml @@ -1,142 +1,96 @@ - 4.0.0 org.baeldung.spring SpringMVCEmail + 1.0-SNAPSHOT war - 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + - - javax.servlet - javax.servlet-api - 3.0.1 + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-mail + 1.4.0.RELEASE + + + org.springframework.boot + spring-boot-starter-tomcat provided - - - javax.servlet - jstl - 1.2 + + com.jayway.jsonpath + json-path + test + --> + + + - - javax.validation - validation-api - 1.1.0.Final - - - - - org.hibernate - hibernate-validator - 5.0.1.Final - - - - - org.springframework.data - spring-data-jpa - 1.10.1.RELEASE - - - - - org.springframework.security - spring-security-web - 4.0.4.RELEASE - - - org.springframework.security - spring-security-config - 4.0.4.RELEASE - - - - - com.sun.mail - javax.mail - 1.5.5 - - - - - org.springframework.integration - spring-integration-mail - 4.3.0.RELEASE - + 4.3.2.RELEASE + --> + + 1.8 + + + - SpringMVCEmail - - - org.apache.tomcat.maven - tomcat6-maven-plugin - 2.2 - - http://localhost:8080/manager/text - TomcatServer - /SpringMVCEmail - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - - - - - org.apache.maven.plugins - maven-war-plugin - 2.4 - - src/main/webapp/WEB-INF/web.xml - - - src/main/resources/META-INF - - - + org.springframework.boot + spring-boot-maven-plugin - \ No newline at end of file + + + + spring-releases + https://repo.spring.io/libs-release + + + + + spring-releases + https://repo.spring.io/libs-release + + + diff --git a/spring-mvc-email/pom_old.xml b/spring-mvc-email/pom_old.xml new file mode 100644 index 0000000000..0ac84f5cc9 --- /dev/null +++ b/spring-mvc-email/pom_old.xml @@ -0,0 +1,142 @@ + + + 4.0.0 + + org.baeldung.spring + SpringMVCEmail + war + 1.0 + + + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + + javax.servlet + jstl + 1.2 + + + + + org.springframework + spring-context + 4.2.5.RELEASE + + + + org.springframework + spring-core + 4.2.5.RELEASE + + + + org.springframework + spring-web + 4.2.5.RELEASE + + + + org.springframework + spring-webmvc + 4.2.5.RELEASE + + + + + javax.validation + validation-api + 1.1.0.Final + + + + + org.hibernate + hibernate-validator + 5.0.1.Final + + + + + org.springframework.data + spring-data-jpa + 1.10.1.RELEASE + + + + + org.springframework.security + spring-security-web + 4.0.4.RELEASE + + + org.springframework.security + spring-security-config + 4.0.4.RELEASE + + + + + com.sun.mail + javax.mail + 1.5.5 + + + + + org.springframework.integration + spring-integration-mail + 4.3.0.RELEASE + + + + + SpringMVCEmail + + + + + org.apache.tomcat.maven + tomcat6-maven-plugin + 2.2 + + http://localhost:8080/manager/text + TomcatServer + /SpringMVCEmail + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-war-plugin + 2.4 + + src/main/webapp/WEB-INF/web.xml + + + src/main/resources/META-INF + + + + + + + \ No newline at end of file diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java new file mode 100644 index 0000000000..0ab568e111 --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java @@ -0,0 +1,35 @@ +package com.baeldung.spring; + +import com.baeldung.spring.mail.EmailServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.mail.SimpleMailMessage; + +import javax.annotation.PostConstruct; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + + } + + @Autowired + public EmailServiceImpl emailService; + + @PostConstruct + public void postConstruct() { + sendSimpleEmail(); + } + + private void sendSimpleEmail() { + + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom("from@test.com"); + message.setTo("to@test.com"); + message.setSubject("Test Message"); + + emailService.sendMail(message); + } +} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java similarity index 89% rename from spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java rename to spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java index 4c5286490b..4dcf9c1b7d 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/config/AppConfig.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java @@ -1,24 +1,19 @@ -package com.baeldung.spring.config; +package com.baeldung.spring.app.config; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; import org.springframework.mail.SimpleMailMessage; -import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.UrlBasedViewResolver; -import java.util.Properties; - /** * Created with IntelliJ IDEA. * User: Olga */ -@Configuration -@ComponentScan("com.baeldung.spring") -@EnableWebMvc //tha same as +//@Configuration +//@ComponentScan("com.baeldung.spring") +//@EnableWebMvc //tha same as public class AppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { @@ -47,7 +42,7 @@ public class AppConfig extends WebMvcConfigurerAdapter { } /* Gmail */ - @Bean + /*@Bean public JavaMailSenderImpl mailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.gmail.com"); @@ -63,7 +58,7 @@ public class AppConfig extends WebMvcConfigurerAdapter { props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.starttls.enable", "true"); return props; - } + }*/ @Bean public SimpleMailMessage templateMessage() { diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/application.properties b/spring-mvc-email/src/main/java/com/baeldung/spring/application.properties new file mode 100644 index 0000000000..573f901152 --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/application.properties @@ -0,0 +1,4 @@ +spring.mail.host=smtp.gmail.com +spring.mail.port=25 +spring.mail.username=test +spring.mail.password=test \ No newline at end of file diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java index 656e237a9e..234593f02e 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java @@ -3,17 +3,19 @@ package com.baeldung.spring.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; /** * Created with IntelliJ IDEA. * User: Olga */ -@Controller +@RestController @RequestMapping({"/","/home"}) public class HomeController { @RequestMapping(method = RequestMethod.GET) - public String showHomePage() { - return "home"; + public ModelAndView showHomePage() { + return new ModelAndView("home"); } } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java index 5de722f444..90f628f586 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java @@ -1,17 +1,7 @@ package com.baeldung.spring.controllers; -import com.baeldung.spring.mail.MailService; -import com.baeldung.spring.web.dto.MailObject; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.Errors; -import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - -import javax.validation.Valid; /** * Created by Olga on 7/20/2016. @@ -19,8 +9,8 @@ import javax.validation.Valid; @Controller @RequestMapping("/mail") public class MailController { - @Autowired - public MailService mailService; + /*@Autowired + public EMailService mailService; @RequestMapping(value = "/send", method = RequestMethod.GET) public String createMail(Model model) { @@ -40,5 +30,5 @@ public class MailController { mailService.sendMail(mailMessage); return "redirect:/home"; - } + }*/ } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Application.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Application.java deleted file mode 100644 index 68f89f778d..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Application.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.spring.mail; - -import org.springframework.mail.SimpleMailMessage; - -/** - * Created by Olga on 7/15/2016. - */ -public class Application { - - public static void main(String ...args) { - MailService mailService = new MailService(); - - SimpleMailMessage message = new SimpleMailMessage(); - message.setFrom("from@test.com"); - message.setTo("to@test.com"); - message.setSubject("Test Message"); - - mailService.sendMail(message); - } -} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/MailService.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java similarity index 81% rename from spring-mvc-email/src/main/java/com/baeldung/spring/mail/MailService.java rename to spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java index 4deab12e0a..bba646afcc 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/MailService.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -2,11 +2,13 @@ package com.baeldung.spring.mail; import com.baeldung.spring.web.dto.MailObject; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import javax.mail.MessagingException; @@ -16,21 +18,23 @@ import java.io.File; /** * Created by Olga on 7/15/2016. */ -@Service -public class MailService { - @Autowired - public JavaMailSender mailSender; +@Component +@ConditionalOnClass(JavaMailSender.class) +public class EmailServiceImpl { - public void sendMail(MimeMessage message) { + @Autowired + public JavaMailSender emailSender; + + /*public void sendMail(MimeMessage message) { try { - mailSender.send(message); + emailSender.send(message); } catch (MailException exception) { exception.printStackTrace(); } } public MimeMessage createMessageWithAttachment(MailObject mailObject) { - MimeMessage message = mailSender.createMimeMessage(); + MimeMessage message = emailSender.createMimeMessage(); try { // pass 'true' to the constructor to create a multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); @@ -47,17 +51,17 @@ public class MailService { e.printStackTrace(); } return message; - } + }*/ public void sendMail(SimpleMailMessage message) { try { - mailSender.send(message); + emailSender.send(message); } catch (MailException exception) { exception.printStackTrace(); } } - @Autowired + /*@Autowired public SimpleMailMessage template; public SimpleMailMessage createSimpleMailMessage(MailObject mailObject) { @@ -69,5 +73,5 @@ public class MailService { mailMessage.setText(String.format(template.getText(), mailObject.getText())); return mailMessage; - } + }*/ } diff --git a/spring-mvc-email/src/main/resources/META-INF/application.xml b/spring-mvc-email/src/main/resources/META-INF/application.xml index f2d901b497..759a312bd4 100644 --- a/spring-mvc-email/src/main/resources/META-INF/application.xml +++ b/spring-mvc-email/src/main/resources/META-INF/application.xml @@ -13,7 +13,7 @@ web.war - SpringMVCEmail + SpringMVCEmailWeb \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml b/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml new file mode 100644 index 0000000000..5a3bb2377a --- /dev/null +++ b/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml index 5fe46d45d6..b979e00276 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml @@ -8,7 +8,7 @@ simpleweb org.springframework.web.servlet.DispatcherServlet - + 1 @@ -25,4 +25,17 @@ simpleweb / + + + + org.springframework.web.context.ContextLoaderListener + + + + + contextConfigLocation + + /WEB-INF/simpleweb-servlet.xml + + From faf368d10c4ac7d086eb7f2b9d1a02226fa7ea5d Mon Sep 17 00:00:00 2001 From: oreva Date: Mon, 22 Aug 2016 23:58:18 +0300 Subject: [PATCH 05/13] Spring-boot-starter-mail provided succsessfully. --- spring-mvc-email/pom.xml | 59 +++---------------- .../java/com/baeldung/spring/Application.java | 25 ++++---- .../baeldung/spring/app/config/AppConfig.java | 29 ++++----- .../baeldung/spring/application.properties | 4 -- .../spring/controllers/HomeController.java | 6 +- .../spring/controllers/MailController.java | 23 +++++--- .../baeldung/spring/mail/EmailService.java | 20 +++++++ .../spring/mail/EmailServiceImpl.java | 57 ++++++++++++++---- .../java/com/baeldung/spring/mail/Order.java | 43 ++++++++++++++ .../baeldung/spring/mail/OrderManager.java | 29 +++++++++ .../src/main/resources/application.properties | 20 +++++++ .../src/main/webapp/WEB-INF/web.xml | 8 +-- 12 files changed, 216 insertions(+), 107 deletions(-) delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/application.properties create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailService.java create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/Order.java create mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java create mode 100644 spring-mvc-email/src/main/resources/application.properties diff --git a/spring-mvc-email/pom.xml b/spring-mvc-email/pom.xml index 6b2ef0bf9b..0d3acec1fe 100644 --- a/spring-mvc-email/pom.xml +++ b/spring-mvc-email/pom.xml @@ -5,7 +5,7 @@ org.baeldung.spring SpringMVCEmail - 1.0-SNAPSHOT + 1.0 war @@ -20,51 +20,23 @@ org.springframework.boot spring-boot-starter-web + org.springframework.boot spring-boot-starter-mail 1.4.0.RELEASE + - org.springframework.boot - spring-boot-starter-tomcat - provided - - - - - @@ -80,17 +52,4 @@ - - - - spring-releases - https://repo.spring.io/libs-release - - - - - spring-releases - https://repo.spring.io/libs-release - - diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java index 0ab568e111..769658fdde 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java @@ -1,9 +1,14 @@ package com.baeldung.spring; +import com.baeldung.spring.mail.EmailService; import com.baeldung.spring.mail.EmailServiceImpl; +import com.baeldung.spring.mail.Order; +import com.baeldung.spring.mail.OrderManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ImportResource; import org.springframework.mail.SimpleMailMessage; import javax.annotation.PostConstruct; @@ -12,24 +17,18 @@ import javax.annotation.PostConstruct; public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); - } @Autowired - public EmailServiceImpl emailService; + public EmailService emailService; + + @Autowired + public OrderManager orderManager; @PostConstruct public void postConstruct() { - sendSimpleEmail(); - } - - private void sendSimpleEmail() { - - SimpleMailMessage message = new SimpleMailMessage(); - message.setFrom("from@test.com"); - message.setTo("to@test.com"); - message.setSubject("Test Message"); - - emailService.sendMail(message); + //emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message"); + Order order = new Order("reva.olga@gmail.com", "First Name", "Last Name"); + orderManager.placeOrder(order); } } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java index 4dcf9c1b7d..0c1662a36c 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java @@ -1,6 +1,8 @@ package com.baeldung.spring.app.config; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; import org.springframework.mail.SimpleMailMessage; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.view.InternalResourceViewResolver; @@ -11,9 +13,9 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver; * Created with IntelliJ IDEA. * User: Olga */ -//@Configuration -//@ComponentScan("com.baeldung.spring") -//@EnableWebMvc //tha same as +@Configuration +@ComponentScan("com.baeldung.spring") +@EnableWebMvc //tha same as public class AppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { @@ -41,6 +43,16 @@ public class AppConfig extends WebMvcConfigurerAdapter { return resolver; } + @Bean + public SimpleMailMessage templateMessage() { + SimpleMailMessage message = new SimpleMailMessage(); + message.setText("Dear %s %s, \nthank you for placing order.\n" + + "\n" + + "Sincerely yours,\n" + + "Yourcompany."); + return message; + } + /* Gmail */ /*@Bean public JavaMailSenderImpl mailSender() { @@ -60,17 +72,6 @@ public class AppConfig extends WebMvcConfigurerAdapter { return props; }*/ - @Bean - public SimpleMailMessage templateMessage() { - SimpleMailMessage message = new SimpleMailMessage(); - message.setText("This is automatically generated email,\n" + - "Original mail text is included : %s\n" + - "\n" + - "Sincerely yours,\n" + - "Yourcompany."); - return message; - } - /*Amazon SES @Bean public JavaMailSenderImpl mailSender() { diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/application.properties b/spring-mvc-email/src/main/java/com/baeldung/spring/application.properties deleted file mode 100644 index 573f901152..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/application.properties +++ /dev/null @@ -1,4 +0,0 @@ -spring.mail.host=smtp.gmail.com -spring.mail.port=25 -spring.mail.username=test -spring.mail.password=test \ No newline at end of file diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java index 234593f02e..50643684d1 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java @@ -10,12 +10,12 @@ import org.springframework.web.servlet.ModelAndView; * Created with IntelliJ IDEA. * User: Olga */ -@RestController +@Controller @RequestMapping({"/","/home"}) public class HomeController { @RequestMapping(method = RequestMethod.GET) - public ModelAndView showHomePage() { - return new ModelAndView("home"); + public String showHomePage() { + return "home"; } } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java index 90f628f586..79152797ff 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java @@ -1,7 +1,17 @@ package com.baeldung.spring.controllers; +import com.baeldung.spring.mail.EmailServiceImpl; +import com.baeldung.spring.web.dto.MailObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.Errors; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import javax.validation.Valid; /** * Created by Olga on 7/20/2016. @@ -9,8 +19,8 @@ import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/mail") public class MailController { - /*@Autowired - public EMailService mailService; + @Autowired + public EmailServiceImpl emailService; @RequestMapping(value = "/send", method = RequestMethod.GET) public String createMail(Model model) { @@ -22,13 +32,8 @@ public class MailController { public String createMail(Model model, @ModelAttribute("mailObject") @Valid MailObject mailObject, Errors errors) { - if(errors.hasErrors()) { - return "mail/send"; - } - - SimpleMailMessage mailMessage = mailService.createSimpleMailMessage(mailObject); - mailService.sendMail(mailMessage); + emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message"); return "redirect:/home"; - }*/ + } } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailService.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailService.java new file mode 100644 index 0000000000..43d7378227 --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailService.java @@ -0,0 +1,20 @@ +package com.baeldung.spring.mail; + +import org.springframework.mail.SimpleMailMessage; + +/** + * Created by Olga on 8/22/2016. + */ +public interface EmailService { + void sendSimpleMessage(String to, + String subject, + String text); + void sendSimpleMessageUsingTemplate(String to, + String subject, + SimpleMailMessage template, + String ...templateArgs); + void sendMessageWithAttachment(String to, + String subject, + String text, + String pathToAttachment); +} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java index bba646afcc..a6bcb00943 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -19,12 +19,57 @@ import java.io.File; * Created by Olga on 7/15/2016. */ @Component -@ConditionalOnClass(JavaMailSender.class) -public class EmailServiceImpl { +public class EmailServiceImpl implements EmailService { @Autowired public JavaMailSender emailSender; + public void sendSimpleMessage(String to, String subject, String text) { + try { + SimpleMailMessage message = new SimpleMailMessage(); + + message.setTo(to); + message.setSubject(subject); + message.setText(text); + + emailSender.send(message); + } catch (MailException exception) { + exception.printStackTrace(); + } + } + + @Override + public void sendSimpleMessageUsingTemplate(String to, + String subject, + SimpleMailMessage template, + String ...templateArgs) { + String text = String.format(template.getText(), templateArgs); + sendSimpleMessage(to, subject, text); + } + + @Override + public void sendMessageWithAttachment(String to, + String subject, + String text, + String pathToAttachment) { + try { + MimeMessage message = emailSender.createMimeMessage(); + // pass 'true' to the constructor to create a multipart message + MimeMessageHelper helper = new MimeMessageHelper(message, true); + + helper.setTo(to); + helper.setSubject(subject); + helper.setText(text); + + FileSystemResource file = new FileSystemResource(new File(pathToAttachment)); + helper.addAttachment("Invoice", file); + + emailSender.send(message); + } catch (MessagingException e) { + e.printStackTrace(); + } + } + /*public void sendMail(MimeMessage message) { try { emailSender.send(message); @@ -53,14 +98,6 @@ public class EmailServiceImpl { return message; }*/ - public void sendMail(SimpleMailMessage message) { - try { - emailSender.send(message); - } catch (MailException exception) { - exception.printStackTrace(); - } - } - /*@Autowired public SimpleMailMessage template; diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Order.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Order.java new file mode 100644 index 0000000000..cd1448b84b --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Order.java @@ -0,0 +1,43 @@ +package com.baeldung.spring.mail; + +/** + * Created by Olga on 8/22/2016. + */ +public class Order { + + public Order(String customerEmail, + String customerFirstName, + String customerLastName) { + this.customerEmail = customerEmail; + this.customerFirstName = customerFirstName; + this.customerLastName = customerLastName; + } + + private String customerEmail; + private String customerFirstName; + private String customerLastName; + + public String getCustomerEmail() { + return customerEmail; + } + + public void setCustomerEmail(String customerEmail) { + this.customerEmail = customerEmail; + } + + public String getCustomerFirstName() { + return customerFirstName; + } + + public void setCustomerFirstName(String customerFirstName) { + this.customerFirstName = customerFirstName; + } + + public String getCustomerLastName() { + return customerLastName; + } + + public void setCustomerLastName(String customerLastName) { + this.customerLastName = customerLastName; + } +} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java new file mode 100644 index 0000000000..23f6184c8a --- /dev/null +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java @@ -0,0 +1,29 @@ +package com.baeldung.spring.mail; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.stereotype.Component; + +/** + * Created by Olga on 8/22/2016. + */ +@Component +public class OrderManager { + @Autowired + public EmailService emailService; + + @Value("${attachment.invoice}") + private String invoiceAttachmentPath; + + @Autowired + public SimpleMailMessage template; + + public void placeOrder(Order order) { + emailService.sendSimpleMessageUsingTemplate(order.getCustomerEmail(), + "Order Confirmation", + template, + order.getCustomerFirstName(), + order.getCustomerLastName()); + } +} diff --git a/spring-mvc-email/src/main/resources/application.properties b/spring-mvc-email/src/main/resources/application.properties new file mode 100644 index 0000000000..07ded8f46b --- /dev/null +++ b/spring-mvc-email/src/main/resources/application.properties @@ -0,0 +1,20 @@ +# Gmail SMTP +spring.mail.host=smtp.gmail.com +spring.mail.port=587 +spring.mail.username=reva.olga@gmail.com +spring.mail.password=yubtqniqehrimqyf +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true + +# Amazon SES SMTP +#spring.mail.host=email-smtp.us-west-2.amazonaws.com +#spring.mail.username=username +#spring.mail.password=password +#spring.mail.properties.mail.transport.protocol=smtp +#spring.mail.properties.mail.smtp.port=25 +#spring.mail.properties.mail.smtp.auth=true +#spring.mail.properties.mail.smtp.starttls.enable=true +#spring.mail.properties.mail.smtp.starttls.required=true + +# path to attachment file +attachment.invoice=c:/invoice.jpg \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml index b979e00276..cbc1bee20a 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml @@ -8,7 +8,7 @@ simpleweb org.springframework.web.servlet.DispatcherServlet - + 1 @@ -26,7 +26,7 @@ / - + From 31bac637ac766856ca22b09ea21c9c6cdb9a638c Mon Sep 17 00:00:00 2001 From: oreva Date: Tue, 23 Aug 2016 00:04:26 +0300 Subject: [PATCH 06/13] Changed application.properties on defaults for username and pwd. --- spring-mvc-email/src/main/resources/application.properties | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-mvc-email/src/main/resources/application.properties b/spring-mvc-email/src/main/resources/application.properties index 07ded8f46b..61a42050e5 100644 --- a/spring-mvc-email/src/main/resources/application.properties +++ b/spring-mvc-email/src/main/resources/application.properties @@ -1,8 +1,8 @@ # Gmail SMTP spring.mail.host=smtp.gmail.com spring.mail.port=587 -spring.mail.username=reva.olga@gmail.com -spring.mail.password=yubtqniqehrimqyf +spring.mail.username=username +spring.mail.password=password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true @@ -17,4 +17,4 @@ spring.mail.properties.mail.smtp.starttls.enable=true #spring.mail.properties.mail.smtp.starttls.required=true # path to attachment file -attachment.invoice=c:/invoice.jpg \ No newline at end of file +attachment.invoice=path_to_file \ No newline at end of file From 6a599b94068a8dfb33bb8afca2367f9f3a09ce67 Mon Sep 17 00:00:00 2001 From: oreva Date: Mon, 29 Aug 2016 11:49:29 +0300 Subject: [PATCH 07/13] pom_old.xml deleted. --- spring-mvc-email/pom_old.xml | 142 ----------------------------------- 1 file changed, 142 deletions(-) delete mode 100644 spring-mvc-email/pom_old.xml diff --git a/spring-mvc-email/pom_old.xml b/spring-mvc-email/pom_old.xml deleted file mode 100644 index 0ac84f5cc9..0000000000 --- a/spring-mvc-email/pom_old.xml +++ /dev/null @@ -1,142 +0,0 @@ - - - 4.0.0 - - org.baeldung.spring - SpringMVCEmail - war - 1.0 - - - - - javax.servlet - javax.servlet-api - 3.0.1 - provided - - - - javax.servlet - jstl - 1.2 - - - - - org.springframework - spring-context - 4.2.5.RELEASE - - - - org.springframework - spring-core - 4.2.5.RELEASE - - - - org.springframework - spring-web - 4.2.5.RELEASE - - - - org.springframework - spring-webmvc - 4.2.5.RELEASE - - - - - javax.validation - validation-api - 1.1.0.Final - - - - - org.hibernate - hibernate-validator - 5.0.1.Final - - - - - org.springframework.data - spring-data-jpa - 1.10.1.RELEASE - - - - - org.springframework.security - spring-security-web - 4.0.4.RELEASE - - - org.springframework.security - spring-security-config - 4.0.4.RELEASE - - - - - com.sun.mail - javax.mail - 1.5.5 - - - - - org.springframework.integration - spring-integration-mail - 4.3.0.RELEASE - - - - - SpringMVCEmail - - - - - org.apache.tomcat.maven - tomcat6-maven-plugin - 2.2 - - http://localhost:8080/manager/text - TomcatServer - /SpringMVCEmail - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - - - - - org.apache.maven.plugins - maven-war-plugin - 2.4 - - src/main/webapp/WEB-INF/web.xml - - - src/main/resources/META-INF - - - - - - - \ No newline at end of file From 095fe5625a1a50a534f31f094cfbdbd2b133d0a1 Mon Sep 17 00:00:00 2001 From: oreva Date: Tue, 13 Sep 2016 09:35:23 +0300 Subject: [PATCH 08/13] Implemented changes according to the latest comments in my pull request. Now we can send email from the web. Access: http://localhost:8080 --- spring-mvc-email/readme.txt | 6 +++ .../java/com/baeldung/spring/Application.java | 3 +- .../baeldung/spring/app/config/AppConfig.java | 44 ++--------------- .../spring/controllers/MailController.java | 47 ++++++++++++++++++- .../spring/mail/EmailServiceImpl.java | 42 ----------------- .../baeldung/spring/mail/OrderManager.java | 4 +- .../baeldung/spring/web/dto/MailObject.java | 12 ----- .../src/main/resources/application.properties | 3 +- .../main/webapp/WEB-INF/simpleweb-servlet.xml | 23 --------- .../main/webapp/WEB-INF/views/mail/send.jsp | 12 ++--- .../src/main/webapp/WEB-INF/web.xml | 12 ----- 11 files changed, 67 insertions(+), 141 deletions(-) create mode 100644 spring-mvc-email/readme.txt delete mode 100644 spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml diff --git a/spring-mvc-email/readme.txt b/spring-mvc-email/readme.txt new file mode 100644 index 0000000000..24ff5438d7 --- /dev/null +++ b/spring-mvc-email/readme.txt @@ -0,0 +1,6 @@ +You can send test email in several ways: +First of all, you should set application.properties properly. +1. Just build the project and the test email for order confirmation will be sent on Spring Boot Start. +2. You can use the web form and test all the cases of sending email in your browser. +Just start the application then type http://localhost:8080 and follow the web flow. +You can send simple email, email with template, email with attachment from the web. \ No newline at end of file diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java index 769658fdde..7d92c2964a 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java @@ -27,8 +27,7 @@ public class Application { @PostConstruct public void postConstruct() { - //emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message"); - Order order = new Order("reva.olga@gmail.com", "First Name", "Last Name"); + Order order = new Order("user_email_address", "First Name", "Last Name"); orderManager.placeOrder(order); } } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java index 0c1662a36c..4fb684c2b6 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java @@ -44,7 +44,7 @@ public class AppConfig extends WebMvcConfigurerAdapter { } @Bean - public SimpleMailMessage templateMessage() { + public SimpleMailMessage templateOrderMessage() { SimpleMailMessage message = new SimpleMailMessage(); message.setText("Dear %s %s, \nthank you for placing order.\n" + "\n" + @@ -53,44 +53,10 @@ public class AppConfig extends WebMvcConfigurerAdapter { return message; } - /* Gmail */ - /*@Bean - public JavaMailSenderImpl mailSender() { - JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); - mailSender.setHost("smtp.gmail.com"); - mailSender.setPort(587); - mailSender.setUsername("username"); - mailSender.setPassword("password"); - mailSender.setJavaMailProperties(javaMailProperties()); - return mailSender; - } - - private Properties javaMailProperties() { - Properties props = new Properties(); - props.setProperty("mail.smtp.auth", "true"); - props.setProperty("mail.smtp.starttls.enable", "true"); - return props; - }*/ - - /*Amazon SES @Bean - public JavaMailSenderImpl mailSender() { - JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); - mailSender.setHost("email-smtp.us-west-2.amazonaws.com"); - mailSender.setUsername("username"); - mailSender.setPassword("password"); - mailSender.setJavaMailProperties(javaMailProperties()); - return mailSender; + public SimpleMailMessage templateSimpleMessage() { + SimpleMailMessage message = new SimpleMailMessage(); + message.setText("This is the test email template for your email:\n%s\n"); + return message; } - - private Properties javaMailProperties() { - Properties props = new Properties(); - props.setProperty("mail.transport.protocol", "smtps"); - props.setProperty("mail.smtp.port", "25"); - - props.put("mail.smtp.auth", "true"); - props.put("mail.smtp.starttls.enable", "true"); - props.put("mail.smtp.starttls.required", "true"); - return props; - }*/ } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java index 79152797ff..832a60ba5d 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java @@ -3,6 +3,8 @@ package com.baeldung.spring.controllers; import com.baeldung.spring.mail.EmailServiceImpl; import com.baeldung.spring.web.dto.MailObject; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -22,6 +24,13 @@ public class MailController { @Autowired public EmailServiceImpl emailService; + @Value("${attachment.invoice}") + private String attachmentPath; + + @Autowired + @Qualifier("templateSimpleMessage") + public SimpleMailMessage template; + @RequestMapping(value = "/send", method = RequestMethod.GET) public String createMail(Model model) { model.addAttribute("mailObject", new MailObject()); @@ -32,7 +41,43 @@ public class MailController { public String createMail(Model model, @ModelAttribute("mailObject") @Valid MailObject mailObject, Errors errors) { - emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message"); + if (errors.hasErrors()) { + return "mail/send"; + } + emailService.sendSimpleMessage(mailObject.getTo(), + mailObject.getSubject(), mailObject.getText()); + + return "redirect:/home"; + } + + @RequestMapping(value = "/sendTemplate", method = RequestMethod.POST) + public String createMailWithTemplate(Model model, + @ModelAttribute("mailObject") @Valid MailObject mailObject, + Errors errors) { + if (errors.hasErrors()) { + return "mail/send"; + } + emailService.sendSimpleMessageUsingTemplate(mailObject.getTo(), + mailObject.getSubject(), + template, + mailObject.getText()); + + return "redirect:/home"; + } + + @RequestMapping(value = "/sendAttachment", method = RequestMethod.POST) + public String createMailWithAttachment(Model model, + @ModelAttribute("mailObject") @Valid MailObject mailObject, + Errors errors) { + if (errors.hasErrors()) { + return "mail/send"; + } + emailService.sendMessageWithAttachment( + mailObject.getTo(), + mailObject.getSubject(), + mailObject.getText(), + attachmentPath + ); return "redirect:/home"; } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java index a6bcb00943..8ecde598f9 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -69,46 +69,4 @@ public class EmailServiceImpl implements EmailService { e.printStackTrace(); } } - - /*public void sendMail(MimeMessage message) { - try { - emailSender.send(message); - } catch (MailException exception) { - exception.printStackTrace(); - } - } - - public MimeMessage createMessageWithAttachment(MailObject mailObject) { - MimeMessage message = emailSender.createMimeMessage(); - try { - // pass 'true' to the constructor to create a multipart message - MimeMessageHelper helper = new MimeMessageHelper(message, true); - - helper.setFrom(mailObject.getFrom()); - helper.setTo(mailObject.getTo()); - helper.setSubject(mailObject.getSubject()); - helper.setText(mailObject.getText()); - - // attach a sample image attachment - FileSystemResource file = new FileSystemResource(new File("c:/attachment.jpg")); - helper.addAttachment("Attachment.jpg", file); - } catch (MessagingException e) { - e.printStackTrace(); - } - return message; - }*/ - - /*@Autowired - public SimpleMailMessage template; - - public SimpleMailMessage createSimpleMailMessage(MailObject mailObject) { - SimpleMailMessage mailMessage = new SimpleMailMessage(template); - - mailMessage.setFrom(mailObject.getFrom()); - mailMessage.setTo(mailObject.getTo()); - mailMessage.setSubject(mailObject.getSubject()); - mailMessage.setText(String.format(template.getText(), mailObject.getText())); - - return mailMessage; - }*/ } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java index 23f6184c8a..5dfe25f20f 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java @@ -1,6 +1,7 @@ package com.baeldung.spring.mail; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Component; @@ -16,7 +17,8 @@ public class OrderManager { @Value("${attachment.invoice}") private String invoiceAttachmentPath; - @Autowired + @Autowired() + @Qualifier("templateOrderMessage") public SimpleMailMessage template; public void placeOrder(Order order) { diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java b/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java index 3369ccd9f6..9623ff5d78 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java @@ -9,10 +9,6 @@ import javax.validation.constraints.Size; * Created by Olga on 7/20/2016. */ public class MailObject { - @Email - @NotNull - @Size(min = 1, message = "Please, specify your email address") - private String from; @Email @NotNull @Size(min = 1, message = "Please, set an email address to send the message to it") @@ -20,14 +16,6 @@ public class MailObject { private String subject; private String text; - public String getFrom() { - return from; - } - - public void setFrom(String from) { - this.from = from; - } - public String getTo() { return to; } diff --git a/spring-mvc-email/src/main/resources/application.properties b/spring-mvc-email/src/main/resources/application.properties index 61a42050e5..ba0608c3af 100644 --- a/spring-mvc-email/src/main/resources/application.properties +++ b/spring-mvc-email/src/main/resources/application.properties @@ -17,4 +17,5 @@ spring.mail.properties.mail.smtp.starttls.enable=true #spring.mail.properties.mail.smtp.starttls.required=true # path to attachment file -attachment.invoice=path_to_file \ No newline at end of file +attachment.invoice=path_to_file +#attachment.invoice=c:/invoice.jpg \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml b/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml deleted file mode 100644 index 5a3bb2377a..0000000000 --- a/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp index 9e9cbc1ff7..1307fc2baf 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp +++ b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp @@ -24,13 +24,6 @@ - - - - Enter your email address
- - - @@ -48,8 +41,11 @@ + - Send + + + diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml index cbc1bee20a..4cd41216d9 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml @@ -26,16 +26,4 @@ / - From b86dd316bb7cc920c96909e939152daeb3bf266f Mon Sep 17 00:00:00 2001 From: oreva Date: Sun, 18 Sep 2016 20:47:55 +0300 Subject: [PATCH 09/13] README.md file added. --- spring-mvc-email/README.md | 13 +++++++++++++ spring-mvc-email/readme.txt | 6 ------ 2 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 spring-mvc-email/README.md delete mode 100644 spring-mvc-email/readme.txt diff --git a/spring-mvc-email/README.md b/spring-mvc-email/README.md new file mode 100644 index 0000000000..0de6532393 --- /dev/null +++ b/spring-mvc-email/README.md @@ -0,0 +1,13 @@ +## Spring MVC Email + +Example Spring MVC project to send email from web form. + +### Installing and Running + +Just run the Spring Boot application. +Type http://localhost:8080 in your browser to open the application. + + +### Sending test emails + +Follow UI links to send simple email, email using template or email with attachment. \ No newline at end of file diff --git a/spring-mvc-email/readme.txt b/spring-mvc-email/readme.txt deleted file mode 100644 index 24ff5438d7..0000000000 --- a/spring-mvc-email/readme.txt +++ /dev/null @@ -1,6 +0,0 @@ -You can send test email in several ways: -First of all, you should set application.properties properly. -1. Just build the project and the test email for order confirmation will be sent on Spring Boot Start. -2. You can use the web form and test all the cases of sending email in your browser. -Just start the application then type http://localhost:8080 and follow the web flow. -You can send simple email, email with template, email with attachment from the web. \ No newline at end of file From 2f4ac59c54966294d01756446cff09e6170a03b0 Mon Sep 17 00:00:00 2001 From: oreva Date: Sun, 18 Sep 2016 20:53:09 +0300 Subject: [PATCH 10/13] Unused imports removed; Application filed cleaned up from the logic of sending email on startup. --- .../java/com/baeldung/spring/Application.java | 21 ------------------- .../baeldung/spring/app/config/AppConfig.java | 4 +++- .../spring/controllers/HomeController.java | 2 -- .../spring/mail/EmailServiceImpl.java | 3 --- 4 files changed, 3 insertions(+), 27 deletions(-) diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java index 7d92c2964a..f146ee1d04 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java @@ -1,33 +1,12 @@ package com.baeldung.spring; -import com.baeldung.spring.mail.EmailService; -import com.baeldung.spring.mail.EmailServiceImpl; -import com.baeldung.spring.mail.Order; -import com.baeldung.spring.mail.OrderManager; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.support.SpringBootServletInitializer; -import org.springframework.context.annotation.ImportResource; -import org.springframework.mail.SimpleMailMessage; -import javax.annotation.PostConstruct; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } - - @Autowired - public EmailService emailService; - - @Autowired - public OrderManager orderManager; - - @PostConstruct - public void postConstruct() { - Order order = new Order("user_email_address", "First Name", "Last Name"); - orderManager.placeOrder(order); - } } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java index 4fb684c2b6..9f8339c2fe 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java @@ -4,7 +4,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.mail.SimpleMailMessage; -import org.springframework.web.servlet.config.annotation.*; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.UrlBasedViewResolver; diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java index 50643684d1..656e237a9e 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java @@ -3,8 +3,6 @@ package com.baeldung.spring.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.ModelAndView; /** * Created with IntelliJ IDEA. diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java index 8ecde598f9..dab5400d25 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -1,15 +1,12 @@ package com.baeldung.spring.mail; -import com.baeldung.spring.web.dto.MailObject; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; From 309e1a2da75e769bd2ff5bb9ec747e55bc2ac581 Mon Sep 17 00:00:00 2001 From: oreva Date: Thu, 6 Oct 2016 12:53:00 +0300 Subject: [PATCH 11/13] Options to send different types of emails were moved to the home page. --- .../spring/controllers/MailController.java | 2 +- .../src/main/webapp/WEB-INF/views/home.jsp | 24 ++++++++++++++++--- .../main/webapp/WEB-INF/views/mail/send.jsp | 4 +--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java index 832a60ba5d..1f536e3297 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java @@ -31,7 +31,7 @@ public class MailController { @Qualifier("templateSimpleMessage") public SimpleMailMessage template; - @RequestMapping(value = "/send", method = RequestMethod.GET) + @RequestMapping(value = {"/send", "/sendTemplate", "/sendAttachment"}, method = RequestMethod.GET) public String createMail(Model model) { model.addAttribute("mailObject", new MailObject()); return "mail/send"; diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp b/spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp index 74165f2626..63351bbf3a 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp +++ b/spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp @@ -15,9 +15,27 @@
-

Send Email

-
- +

Select any of the options below to send sample email:

+ +
+ + + + + + + + + + +
+ +
+ +
+ +
+
diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp index 1307fc2baf..294cb2c49c 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp +++ b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp @@ -43,9 +43,7 @@ - - - + From 4d46d5273386270c738f910036f9b0c500d9dfa6 Mon Sep 17 00:00:00 2001 From: oreva Date: Sun, 6 Nov 2016 10:23:19 +0200 Subject: [PATCH 12/13] Patch 0001-Code-cleanup.patch applied. --- .../baeldung/spring/app/config/AppConfig.java | 10 ----- .../spring/mail/EmailServiceImpl.java | 3 +- .../java/com/baeldung/spring/mail/Order.java | 43 ------------------- .../baeldung/spring/mail/OrderManager.java | 31 ------------- 4 files changed, 1 insertion(+), 86 deletions(-) delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/Order.java delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java index 9f8339c2fe..9078d44764 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java @@ -45,16 +45,6 @@ public class AppConfig extends WebMvcConfigurerAdapter { return resolver; } - @Bean - public SimpleMailMessage templateOrderMessage() { - SimpleMailMessage message = new SimpleMailMessage(); - message.setText("Dear %s %s, \nthank you for placing order.\n" + - "\n" + - "Sincerely yours,\n" + - "Yourcompany."); - return message; - } - @Bean public SimpleMailMessage templateSimpleMessage() { SimpleMailMessage message = new SimpleMailMessage(); diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java index dab5400d25..ca418a7d90 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -24,7 +24,6 @@ public class EmailServiceImpl implements EmailService { public void sendSimpleMessage(String to, String subject, String text) { try { SimpleMailMessage message = new SimpleMailMessage(); - message.setTo(to); message.setSubject(subject); message.setText(text); @@ -40,7 +39,7 @@ public class EmailServiceImpl implements EmailService { String subject, SimpleMailMessage template, String ...templateArgs) { - String text = String.format(template.getText(), templateArgs); + String text = String.format(template.getText(), templateArgs); sendSimpleMessage(to, subject, text); } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Order.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Order.java deleted file mode 100644 index cd1448b84b..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/Order.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.spring.mail; - -/** - * Created by Olga on 8/22/2016. - */ -public class Order { - - public Order(String customerEmail, - String customerFirstName, - String customerLastName) { - this.customerEmail = customerEmail; - this.customerFirstName = customerFirstName; - this.customerLastName = customerLastName; - } - - private String customerEmail; - private String customerFirstName; - private String customerLastName; - - public String getCustomerEmail() { - return customerEmail; - } - - public void setCustomerEmail(String customerEmail) { - this.customerEmail = customerEmail; - } - - public String getCustomerFirstName() { - return customerFirstName; - } - - public void setCustomerFirstName(String customerFirstName) { - this.customerFirstName = customerFirstName; - } - - public String getCustomerLastName() { - return customerLastName; - } - - public void setCustomerLastName(String customerLastName) { - this.customerLastName = customerLastName; - } -} diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java deleted file mode 100644 index 5dfe25f20f..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.spring.mail; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.mail.SimpleMailMessage; -import org.springframework.stereotype.Component; - -/** - * Created by Olga on 8/22/2016. - */ -@Component -public class OrderManager { - @Autowired - public EmailService emailService; - - @Value("${attachment.invoice}") - private String invoiceAttachmentPath; - - @Autowired() - @Qualifier("templateOrderMessage") - public SimpleMailMessage template; - - public void placeOrder(Order order) { - emailService.sendSimpleMessageUsingTemplate(order.getCustomerEmail(), - "Order Confirmation", - template, - order.getCustomerFirstName(), - order.getCustomerLastName()); - } -} From 807a4d2db9fadd1df4976d8b5b3e0d9fd0c30813 Mon Sep 17 00:00:00 2001 From: oreva Date: Sun, 6 Nov 2016 12:07:51 +0200 Subject: [PATCH 13/13] Implemented changes regarding the latest review. --- .../spring/controllers/MailController.java | 49 ++++++++++++- .../src/main/resources/application.properties | 3 +- .../main/webapp/WEB-INF/views/mail/send.jsp | 70 ++++++++++--------- 3 files changed, 86 insertions(+), 36 deletions(-) diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java index 1f536e3297..768a0f8e7b 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java @@ -13,7 +13,12 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; /** * Created by Olga on 7/20/2016. @@ -31,8 +36,50 @@ public class MailController { @Qualifier("templateSimpleMessage") public SimpleMailMessage template; + private static final Map> labels; + + static { + labels = new HashMap<>(); + + //Simple email + Map props = new HashMap<>(); + props.put("headerText", "Send Simple Email"); + props.put("messageLabel", "Message"); + props.put("additionalInfo", ""); + labels.put("send", props); + + //Email with template + props = new HashMap<>(); + props.put("headerText", "Send Email Using Template"); + props.put("messageLabel", "Template Parameter"); + props.put("additionalInfo", + "The parameter value will be added to the following message template:
" + + "This is the test email template for your email:
'Template Parameter'
" + ); + labels.put("sendTemplate", props); + + //Email with attachment + props = new HashMap<>(); + props.put("headerText", "Send Email With Attachment"); + props.put("messageLabel", "Message"); + props.put("additionalInfo", "To make sure that you send an attachment with this email, change the value for the 'attachment.invoice' in the application.properties file to the path to the attachment."); + labels.put("sendAttachment", props); + } + @RequestMapping(value = {"/send", "/sendTemplate", "/sendAttachment"}, method = RequestMethod.GET) - public String createMail(Model model) { + public String createMail(Model model, + HttpServletRequest request) { + String action = request.getRequestURL().substring( + request.getRequestURL().lastIndexOf("/") + 1 + ); + Map props = labels.get(action); + Set keys = props.keySet(); + Iterator iterator = keys.iterator(); + while (iterator.hasNext()) { + String key = iterator.next(); + model.addAttribute(key, props.get(key)); + } + model.addAttribute("mailObject", new MailObject()); return "mail/send"; } diff --git a/spring-mvc-email/src/main/resources/application.properties b/spring-mvc-email/src/main/resources/application.properties index ba0608c3af..61a42050e5 100644 --- a/spring-mvc-email/src/main/resources/application.properties +++ b/spring-mvc-email/src/main/resources/application.properties @@ -17,5 +17,4 @@ spring.mail.properties.mail.smtp.starttls.enable=true #spring.mail.properties.mail.smtp.starttls.required=true # path to attachment file -attachment.invoice=path_to_file -#attachment.invoice=c:/invoice.jpg \ No newline at end of file +attachment.invoice=path_to_file \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp index 294cb2c49c..d27aa09d9a 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp +++ b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp @@ -13,40 +13,44 @@
-

Create Email

- +

${headerText}

+
- - - - - - - - - - - - - - - - - -
- Enter email address
- -
- Enter the subject
- -
- Enter message text
- -
- -
+
+ + + + + + + + + + + + + + + + + +
+ Enter email address
+ +
+ Enter the subject
+ +
+ +
+ +
+
+
+ ${additionalInfo} +