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>
<artifactId>SpringMVCEmail</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0</version>
<packaging>war</packaging>
<parent>
@ -20,51 +20,23 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Mail dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<!-- The following two dependencies included to render jsp pages -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.4</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>-->
<!-- spring-boot-starter-mail -->
<!-- Spring -->
<!--<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</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>
<properties>
@ -80,17 +52,4 @@
</plugin>
</plugins>
</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>

View File

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

View File

@ -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 <mvc:annotation-driven/>
@Configuration
@ComponentScan("com.baeldung.spring")
@EnableWebMvc //tha same as <mvc:annotation-driven/>
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() {

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.
* 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";
}
}

View File

@ -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";
}*/
}
}

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.
*/
@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;

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