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 + +