From 4ff269f58976bf7f2c51f0c73be00ccf80ce0e7f Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 8 Jul 2018 18:20:23 +0300 Subject: [PATCH 1/3] upgrade email article, move to spring-mvc-simple --- spring-mvc-simple/README.md | 1 + spring-mvc-simple/pom.xml | 11 +- .../ApplicationConfiguration.java | 31 +++- .../spring/controller/MailController.java | 132 ++++++++++++++++++ .../baeldung/spring/domain/MailObject.java | 41 ++++++ .../baeldung/spring/mail/EmailService.java | 20 +++ .../spring/mail/EmailServiceImpl.java | 69 +++++++++ .../src/main/resources/application.properties | 22 +++ .../src/main/webapp/WEB-INF/views/emails.jsp | 43 ++++++ .../main/webapp/WEB-INF/views/mail/send.jsp | 58 ++++++++ 10 files changed, 426 insertions(+), 2 deletions(-) create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/controller/MailController.java create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/domain/MailObject.java create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/mail/EmailService.java create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java create mode 100644 spring-mvc-simple/src/main/resources/application.properties create mode 100644 spring-mvc-simple/src/main/webapp/WEB-INF/views/emails.jsp create mode 100644 spring-mvc-simple/src/main/webapp/WEB-INF/views/mail/send.jsp diff --git a/spring-mvc-simple/README.md b/spring-mvc-simple/README.md index 3505fb8009..39053534fa 100644 --- a/spring-mvc-simple/README.md +++ b/spring-mvc-simple/README.md @@ -5,3 +5,4 @@ - [Spring 5 and Servlet 4 – The PushBuilder](http://www.baeldung.com/spring-5-push) - [Servlet Redirect vs Forward](http://www.baeldung.com/servlet-redirect-forward) - [Apache Tiles Integration with Spring MVC](http://www.baeldung.com/spring-mvc-apache-tiles) +- [Guide to Spring Email](http://www.baeldung.com/spring-email) diff --git a/spring-mvc-simple/pom.xml b/spring-mvc-simple/pom.xml index 08eab59540..99611c26fc 100644 --- a/spring-mvc-simple/pom.xml +++ b/spring-mvc-simple/pom.xml @@ -20,6 +20,15 @@ spring-oxm ${spring-oxm.version} + + org.springframework + spring-context-support + + + com.sun.mail + javax.mail + 1.6.1 + javax.servlet javax.servlet-api @@ -171,7 +180,7 @@ 1.2 2.3.2-b02 4.0.0 - 5.4.1.Final + 6.0.10.Final enter-location-of-server 1.3.2 1.8 diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java index c28ee02eef..284be6c212 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java @@ -8,6 +8,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.feed.RssChannelHttpMessageConverter; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; @@ -20,10 +23,11 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; import java.util.ArrayList; import java.util.List; +import java.util.Properties; @Configuration @EnableWebMvc -@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" }) +@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator", "com.baeldung.spring.mail" }) public class ApplicationConfiguration implements WebMvcConfigurer { @Override @@ -60,4 +64,29 @@ public class ApplicationConfiguration implements WebMvcConfigurer { converters.add(new RssChannelHttpMessageConverter()); converters.add(new JsonChannelHttpMessageConverter()); } + + @Bean + public SimpleMailMessage templateSimpleMessage() { + SimpleMailMessage message = new SimpleMailMessage(); + message.setText("This is the test email template for your email:\n%s\n"); + return message; + } + + @Bean + public JavaMailSender getJavaMailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost("smtp.gmail.com"); + mailSender.setPort(587); + + mailSender.setUsername("my.gmail@gmail.com"); + mailSender.setPassword("password"); + + Properties props = mailSender.getJavaMailProperties(); + props.put("mail.transport.protocol", "smtp"); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + props.put("mail.debug", "true"); + + return mailSender; + } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/MailController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/MailController.java new file mode 100644 index 0000000000..16d1202eef --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/MailController.java @@ -0,0 +1,132 @@ +package com.baeldung.spring.controller; + +import com.baeldung.spring.mail.EmailServiceImpl; +import com.baeldung.spring.domain.MailObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +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.servlet.http.HttpServletRequest; +import javax.validation.Valid; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +@Controller +@RequestMapping("/mail") +public class MailController { + + @Autowired + public EmailServiceImpl emailService; + + @Value("${attachment.invoice}") + private String attachmentPath; + + @Autowired + 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(method = RequestMethod.GET) + public String showEmailsPage() { + return "emails"; + } + + @RequestMapping(value = {"/send", "/sendTemplate", "/sendAttachment"}, method = RequestMethod.GET) + 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"; + } + + @RequestMapping(value = "/send", method = RequestMethod.POST) + public String createMail(Model model, + @ModelAttribute("mailObject") @Valid MailObject mailObject, + Errors errors) { + 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-simple/src/main/java/com/baeldung/spring/domain/MailObject.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/domain/MailObject.java new file mode 100644 index 0000000000..aceaf685fa --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/domain/MailObject.java @@ -0,0 +1,41 @@ +package com.baeldung.spring.domain; + +import javax.validation.constraints.Email; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +/** + * Created by Olga on 7/20/2016. + */ +public class MailObject { + @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; + + 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-simple/src/main/java/com/baeldung/spring/mail/EmailService.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/mail/EmailService.java new file mode 100644 index 0000000000..43d7378227 --- /dev/null +++ b/spring-mvc-simple/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-simple/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java new file mode 100644 index 0000000000..039b970d8e --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -0,0 +1,69 @@ +package com.baeldung.spring.mail; + +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.Component; + +import java.io.File; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +/** + * Created by Olga on 7/15/2016. + */ +@Component +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(); + } + } +} diff --git a/spring-mvc-simple/src/main/resources/application.properties b/spring-mvc-simple/src/main/resources/application.properties new file mode 100644 index 0000000000..9a804c07d8 --- /dev/null +++ b/spring-mvc-simple/src/main/resources/application.properties @@ -0,0 +1,22 @@ +#this property file will have to be loaded explicitly as this is not a Spring Boot project + +# Gmail SMTP +spring.mail.host=smtp.gmail.com +spring.mail.port=587 +spring.mail.username=username +spring.mail.password=password +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=path_to_file \ No newline at end of file diff --git a/spring-mvc-simple/src/main/webapp/WEB-INF/views/emails.jsp b/spring-mvc-simple/src/main/webapp/WEB-INF/views/emails.jsp new file mode 100644 index 0000000000..63351bbf3a --- /dev/null +++ b/spring-mvc-simple/src/main/webapp/WEB-INF/views/emails.jsp @@ -0,0 +1,43 @@ +<%-- + 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 + + +
+
+

Select any of the options below to send sample email:

+
+
+ + + + + + + + + + +
+ +
+ +
+ +
+
+
+
+
+ + \ No newline at end of file diff --git a/spring-mvc-simple/src/main/webapp/WEB-INF/views/mail/send.jsp b/spring-mvc-simple/src/main/webapp/WEB-INF/views/mail/send.jsp new file mode 100644 index 0000000000..d27aa09d9a --- /dev/null +++ b/spring-mvc-simple/src/main/webapp/WEB-INF/views/mail/send.jsp @@ -0,0 +1,58 @@ +<%-- + 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 + + +
+

${headerText}

+ +
+
+ + + + + + + + + + + + + + + + + +
+ Enter email address
+ +
+ Enter the subject
+ +
+ +
+ +
+
+
+ ${additionalInfo} +
+
+
+
+ + From 88d17fa432391be7f4886f73c565898343870fe8 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 8 Jul 2018 18:22:27 +0300 Subject: [PATCH 2/3] remove email module --- pom.xml | 1 - spring-mvc-email/README.md | 17 --- spring-mvc-email/pom.xml | 40 ------ .../java/com/baeldung/spring/Application.java | 13 -- .../baeldung/spring/app/config/AppConfig.java | 54 -------- .../spring/controllers/HomeController.java | 19 --- .../spring/controllers/MailController.java | 128 ------------------ .../baeldung/spring/mail/EmailService.java | 20 --- .../spring/mail/EmailServiceImpl.java | 68 ---------- .../baeldung/spring/web/dto/MailObject.java | 42 ------ .../main/resources/META-INF/application.xml | 19 --- .../src/main/resources/application.properties | 20 --- .../src/main/webapp/WEB-INF/views/home.jsp | 43 ------ .../main/webapp/WEB-INF/views/mail/send.jsp | 58 -------- .../src/main/webapp/WEB-INF/web.xml | 29 ---- 15 files changed, 571 deletions(-) delete mode 100644 spring-mvc-email/README.md delete mode 100644 spring-mvc-email/pom.xml delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/Application.java delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailService.java delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java delete mode 100644 spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java delete mode 100644 spring-mvc-email/src/main/resources/META-INF/application.xml delete mode 100644 spring-mvc-email/src/main/resources/application.properties delete mode 100644 spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp delete mode 100644 spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp delete mode 100644 spring-mvc-email/src/main/webapp/WEB-INF/web.xml diff --git a/pom.xml b/pom.xml index 51a762a888..1a0a28f5b8 100644 --- a/pom.xml +++ b/pom.xml @@ -191,7 +191,6 @@ spring-mvc-velocity spring-mvc-webflow spring-mvc-xml - spring-mvc-simple spring-mvc-kotlin spring-security-openid spring-protobuf diff --git a/spring-mvc-email/README.md b/spring-mvc-email/README.md deleted file mode 100644 index aa880188d7..0000000000 --- a/spring-mvc-email/README.md +++ /dev/null @@ -1,17 +0,0 @@ -## Relevant articles: - -- [Guide to Spring Email](http://www.baeldung.com/spring-email) - -## 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. diff --git a/spring-mvc-email/pom.xml b/spring-mvc-email/pom.xml deleted file mode 100644 index 40d83046ef..0000000000 --- a/spring-mvc-email/pom.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - 4.0.0 - - org.baeldung.spring - spring-mvc-email - 1.0 - spring-mvc-email - war - - - parent-boot-1 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-1 - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-tomcat - - - - org.springframework.boot - spring-boot-starter-mail - - - - javax.servlet - jstl - - - - 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 deleted file mode 100644 index bc5d6b3151..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.spring; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.support.SpringBootServletInitializer; - - -@SpringBootApplication -public class Application extends SpringBootServletInitializer { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} 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 deleted file mode 100644 index 9078d44764..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java +++ /dev/null @@ -1,54 +0,0 @@ -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.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; - -/** - * 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; - } - - @Bean - public SimpleMailMessage templateSimpleMessage() { - SimpleMailMessage message = new SimpleMailMessage(); - message.setText("This is the test email template for your email:\n%s\n"); - return message; - } -} 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 deleted file mode 100644 index 656e237a9e..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/HomeController.java +++ /dev/null @@ -1,19 +0,0 @@ -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 deleted file mode 100644 index ff828ca9ec..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java +++ /dev/null @@ -1,128 +0,0 @@ -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; -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.servlet.http.HttpServletRequest; -import javax.validation.Valid; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -@Controller -@RequestMapping("/mail") -public class MailController { - - @Autowired - public EmailServiceImpl emailService; - - @Value("${attachment.invoice}") - private String attachmentPath; - - @Autowired - 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, - 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"; - } - - @RequestMapping(value = "/send", method = RequestMethod.POST) - public String createMail(Model model, - @ModelAttribute("mailObject") @Valid MailObject mailObject, - Errors errors) { - 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/EmailService.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailService.java deleted file mode 100644 index 43d7378227..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailService.java +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index ca418a7d90..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.baeldung.spring.mail; - -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.Component; - -import javax.mail.MessagingException; -import javax.mail.internet.MimeMessage; -import java.io.File; - -/** - * Created by Olga on 7/15/2016. - */ -@Component -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(); - } - } -} 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 deleted file mode 100644 index 9623ff5d78..0000000000 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java +++ /dev/null @@ -1,42 +0,0 @@ -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 - @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; - - 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 deleted file mode 100644 index 759a312bd4..0000000000 --- a/spring-mvc-email/src/main/resources/META-INF/application.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - SpringMVCEmail.war - SpringMVCEmail - - - - - web.war - SpringMVCEmailWeb - - - \ No newline at end of file diff --git a/spring-mvc-email/src/main/resources/application.properties b/spring-mvc-email/src/main/resources/application.properties deleted file mode 100644 index 61a42050e5..0000000000 --- a/spring-mvc-email/src/main/resources/application.properties +++ /dev/null @@ -1,20 +0,0 @@ -# Gmail SMTP -spring.mail.host=smtp.gmail.com -spring.mail.port=587 -spring.mail.username=username -spring.mail.password=password -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=path_to_file \ 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 deleted file mode 100644 index 63351bbf3a..0000000000 --- a/spring-mvc-email/src/main/webapp/WEB-INF/views/home.jsp +++ /dev/null @@ -1,43 +0,0 @@ -<%-- - 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 - - -
-
-

Select any of the options below to send sample 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 deleted file mode 100644 index d27aa09d9a..0000000000 --- a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp +++ /dev/null @@ -1,58 +0,0 @@ -<%-- - 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 - - -
-

${headerText}

- -
-
- - - - - - - - - - - - - - - - - -
- Enter email address
- -
- Enter the subject
- -
- -
- -
-
-
- ${additionalInfo} -
-
-
-
- - diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 4cd41216d9..0000000000 --- a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - simpleweb - org.springframework.web.servlet.DispatcherServlet - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - com.baeldung.spring.app.config.AppConfig - - 1 - - - - simpleweb - / - - - From 28cbbe080f050aedac07b2e03e12c5ec62509829 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 8 Jul 2018 19:17:44 +0300 Subject: [PATCH 3/3] remove email module --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1a0a28f5b8..607d738c15 100644 --- a/pom.xml +++ b/pom.xml @@ -184,7 +184,6 @@ spring-katharsis spring-ldap spring-mockito - spring-mvc-email spring-mvc-forms-jsp spring-mvc-forms-thymeleaf spring-mvc-java