Spring MVC Email application added.

This commit is contained in:
oreva 2016-07-28 18:41:17 +03:00
parent ffcd83697a
commit 24c25206a4
11 changed files with 580 additions and 0 deletions

142
spring-mvc-email/pom.xml Normal file
View File

@ -0,0 +1,142 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung.spring</groupId>
<artifactId>SpringMVCEmail</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<dependencies>
<!-- Because this is a web app, we also have a dependency on the servlet api. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- Bean validation api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.1.RELEASE</version>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- Java Mail library -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
<!-- Spring Email ntegration -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVCEmail</finalName>
<plugins>
<!-- Maven Tomcat Plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/SpringMVCEmail</path>
</configuration>
</plugin>
<!-- Maven compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!-- Maven war plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
<webResources>
<resource>
<directory>src/main/resources/META-INF</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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 <mvc:annotation-driven/>
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;
}*/
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd"
version="6">
<module id="SpringMVCEmail">
<web>
<web-uri>SpringMVCEmail.war</web-uri>
<context-root>SpringMVCEmail</context-root>
</web>
</module>
<module id="SpringMVCEmail-Web">
<web>
<web-uri>web.war</web-uri>
<context-root>SpringMVCEmail</context-root>
</web>
</module>
</application>

View File

@ -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" %>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<div>
<div>
<h4>Send Email</h4>
<form action="mail/send" method="get">
<input type="submit" value="Send Email">
</form>
</div>
</div>
</body>
</html>

View File

@ -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" %>
<html>
<head>
<title>Send Email</title>
</head>
<body>
<div>
<h3>Create Email</h3>
<form:form method="POST" modelAttribute="mailObject">
<fieldset>
<table cellspacing="0">
<tr>
<th><label for="input_to">To</label></th>
<td><form:input path="to" id="input_to" type="email"/>
<small>Enter email address</small><br/>
<form:errors path="to" cssStyle="color:red;font-size:small"/>
</td>
</tr>
<tr>
<th><label for="input_from">From</label></th>
<td><form:input path="from" id="input_from" type="email"/>
<small>Enter your email address</small><br/>
<form:errors path="from" cssStyle="color:red;font-size:small"/>
</td>
</tr>
<tr>
<th><label for="input_subject">Subject</label></th>
<td><form:input path="subject" id="input_subject"/>
<small>Enter the subject</small><br/>
<form:errors path="subject" cssStyle="color:red;font-size:small"/>
</td>
</tr>
<tr>
<th><label for="input_text">Message:</label></th>
<td><form:textarea path="text"
rows="5" cols="50"
id="input_text"/>
<small>Enter message text</small><br/>
<form:errors path="text" cssStyle="color:red;font-size:small"/>
</td>
</tr>
<tr>
<td>
<form:button>Send</form:button>
</td>
</tr>
</table>
</fieldset>
</form:form>
</div>
</body>
</html>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>simpleweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.baeldung.spring.config.AppConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simpleweb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>