Spring-boot-starter-mail provided succsessfully.

This commit is contained in:
oreva 2016-08-22 23:58:18 +03:00
parent 468ccfc216
commit faf368d10c
12 changed files with 216 additions and 107 deletions

View File

@ -5,7 +5,7 @@
<groupId>org.baeldung.spring</groupId> <groupId>org.baeldung.spring</groupId>
<artifactId>SpringMVCEmail</artifactId> <artifactId>SpringMVCEmail</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0</version>
<packaging>war</packaging> <packaging>war</packaging>
<parent> <parent>
@ -20,51 +20,23 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<!-- Spring Boot Starter Mail dependency -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId> <artifactId>spring-boot-starter-mail</artifactId>
<version>1.4.0.RELEASE</version> <version>1.4.0.RELEASE</version>
</dependency> </dependency>
<!-- The following two dependencies included to render jsp pages -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.apache.tomcat.embed</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId> <artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope> <version>8.5.4</version>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.jayway.jsonpath</groupId> <groupId>javax.servlet</groupId>
<artifactId>json-path</artifactId> <artifactId>jstl</artifactId>
<scope>test</scope> <version>1.2</version>
</dependency>-->
<!-- spring-boot-starter-mail -->
<!-- Spring -->
<!--<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>-->
</dependencies> </dependencies>
<properties> <properties>
@ -80,17 +52,4 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project> </project>

View File

@ -1,9 +1,14 @@
package com.baeldung.spring; package com.baeldung.spring;
import com.baeldung.spring.mail.EmailService;
import com.baeldung.spring.mail.EmailServiceImpl; 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.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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 org.springframework.mail.SimpleMailMessage;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -12,24 +17,18 @@ import javax.annotation.PostConstruct;
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
@Autowired @Autowired
public EmailServiceImpl emailService; public EmailService emailService;
@Autowired
public OrderManager orderManager;
@PostConstruct @PostConstruct
public void postConstruct() { public void postConstruct() {
sendSimpleEmail(); //emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message");
} Order order = new Order("reva.olga@gmail.com", "First Name", "Last Name");
orderManager.placeOrder(order);
private void sendSimpleEmail() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("from@test.com");
message.setTo("to@test.com");
message.setSubject("Test Message");
emailService.sendMail(message);
} }
} }

View File

@ -1,6 +1,8 @@
package com.baeldung.spring.app.config; package com.baeldung.spring.app.config;
import org.springframework.context.annotation.Bean; 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.SimpleMailMessage;
import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
@ -11,9 +13,9 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver;
* Created with IntelliJ IDEA. * Created with IntelliJ IDEA.
* User: Olga * User: Olga
*/ */
//@Configuration @Configuration
//@ComponentScan("com.baeldung.spring") @ComponentScan("com.baeldung.spring")
//@EnableWebMvc //tha same as <mvc:annotation-driven/> @EnableWebMvc //tha same as <mvc:annotation-driven/>
public class AppConfig extends WebMvcConfigurerAdapter { public class AppConfig extends WebMvcConfigurerAdapter {
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
@ -41,6 +43,16 @@ public class AppConfig extends WebMvcConfigurerAdapter {
return resolver; 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 */ /* Gmail */
/*@Bean /*@Bean
public JavaMailSenderImpl mailSender() { public JavaMailSenderImpl mailSender() {
@ -60,17 +72,6 @@ public class AppConfig extends WebMvcConfigurerAdapter {
return props; 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 /*Amazon SES
@Bean @Bean
public JavaMailSenderImpl mailSender() { public JavaMailSenderImpl mailSender() {

View File

@ -1,4 +0,0 @@
spring.mail.host=smtp.gmail.com
spring.mail.port=25
spring.mail.username=test
spring.mail.password=test

View File

@ -10,12 +10,12 @@ import org.springframework.web.servlet.ModelAndView;
* Created with IntelliJ IDEA. * Created with IntelliJ IDEA.
* User: Olga * User: Olga
*/ */
@RestController @Controller
@RequestMapping({"/","/home"}) @RequestMapping({"/","/home"})
public class HomeController { public class HomeController {
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)
public ModelAndView showHomePage() { public String showHomePage() {
return new ModelAndView("home"); return "home";
} }
} }

View File

@ -1,7 +1,17 @@
package com.baeldung.spring.controllers; 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.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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;
/** /**
* Created by Olga on 7/20/2016. * Created by Olga on 7/20/2016.
@ -9,8 +19,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller @Controller
@RequestMapping("/mail") @RequestMapping("/mail")
public class MailController { public class MailController {
/*@Autowired @Autowired
public EMailService mailService; public EmailServiceImpl emailService;
@RequestMapping(value = "/send", method = RequestMethod.GET) @RequestMapping(value = "/send", method = RequestMethod.GET)
public String createMail(Model model) { public String createMail(Model model) {
@ -22,13 +32,8 @@ public class MailController {
public String createMail(Model model, public String createMail(Model model,
@ModelAttribute("mailObject") @Valid MailObject mailObject, @ModelAttribute("mailObject") @Valid MailObject mailObject,
Errors errors) { Errors errors) {
if(errors.hasErrors()) { emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message");
return "mail/send";
}
SimpleMailMessage mailMessage = mailService.createSimpleMailMessage(mailObject);
mailService.sendMail(mailMessage);
return "redirect:/home"; return "redirect:/home";
}*/ }
} }

View File

@ -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);
}

View File

@ -19,12 +19,57 @@ import java.io.File;
* Created by Olga on 7/15/2016. * Created by Olga on 7/15/2016.
*/ */
@Component @Component
@ConditionalOnClass(JavaMailSender.class) public class EmailServiceImpl implements EmailService {
public class EmailServiceImpl {
@Autowired @Autowired
public JavaMailSender emailSender; 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) { /*public void sendMail(MimeMessage message) {
try { try {
emailSender.send(message); emailSender.send(message);
@ -53,14 +98,6 @@ public class EmailServiceImpl {
return message; return message;
}*/ }*/
public void sendMail(SimpleMailMessage message) {
try {
emailSender.send(message);
} catch (MailException exception) {
exception.printStackTrace();
}
}
/*@Autowired /*@Autowired
public SimpleMailMessage template; public SimpleMailMessage template;

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

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

View File

@ -8,7 +8,7 @@
<servlet> <servlet>
<servlet-name>simpleweb</servlet-name> <servlet-name>simpleweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--<init-param> <init-param>
<param-name>contextClass</param-name> <param-name>contextClass</param-name>
<param-value> <param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext org.springframework.web.context.support.AnnotationConfigWebApplicationContext
@ -17,7 +17,7 @@
<init-param> <init-param>
<param-name>contextConfigLocation</param-name> <param-name>contextConfigLocation</param-name>
<param-value>com.baeldung.spring.app.config.AppConfig</param-value> <param-value>com.baeldung.spring.app.config.AppConfig</param-value>
</init-param>--> </init-param>
<load-on-startup>1</load-on-startup> <load-on-startup>1</load-on-startup>
</servlet> </servlet>
@ -26,7 +26,7 @@
<url-pattern>/</url-pattern> <url-pattern>/</url-pattern>
</servlet-mapping> </servlet-mapping>
<listener> <!--<listener>
<listener-class> <listener-class>
org.springframework.web.context.ContextLoaderListener org.springframework.web.context.ContextLoaderListener
</listener-class> </listener-class>
@ -37,5 +37,5 @@
<param-value> <param-value>
/WEB-INF/simpleweb-servlet.xml /WEB-INF/simpleweb-servlet.xml
</param-value> </param-value>
</context-param> </context-param>-->
</web-app> </web-app>