remove email module
This commit is contained in:
parent
4ff269f589
commit
88d17fa432
1
pom.xml
1
pom.xml
|
@ -191,7 +191,6 @@
|
|||
<module>spring-mvc-velocity</module>
|
||||
<module>spring-mvc-webflow</module>
|
||||
<module>spring-mvc-xml</module>
|
||||
<module>spring-mvc-simple</module>
|
||||
<module>spring-mvc-kotlin</module>
|
||||
<module>spring-security-openid</module>
|
||||
<module>spring-protobuf</module>
|
||||
|
|
|
@ -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.
|
|
@ -1,40 +0,0 @@
|
|||
<?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>spring-mvc-email</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>spring-mvc-email</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-1</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
</dependency>
|
||||
<!-- Spring Boot Starter Mail dependency -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<!-- The following two dependencies included to render jsp pages -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 <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;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleMailMessage templateSimpleMessage() {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setText("This is the test email template for your email:\n%s\n");
|
||||
return message;
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
}
|
|
@ -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<String, Map<String, String>> labels;
|
||||
|
||||
static {
|
||||
labels = new HashMap<>();
|
||||
|
||||
//Simple email
|
||||
Map<String, String> 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:<br>" +
|
||||
"<b>This is the test email template for your email:<br>'Template Parameter'</b>"
|
||||
);
|
||||
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<String, String> props = labels.get(action);
|
||||
Set<String> keys = props.keySet();
|
||||
Iterator<String> 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";
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
<?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>SpringMVCEmailWeb</context-root>
|
||||
</web>
|
||||
</module>
|
||||
</application>
|
|
@ -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
|
|
@ -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" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Home Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<div>
|
||||
<h4>Select any of the options below to send sample email:</h4>
|
||||
<form method="get" style="width: 200px;">
|
||||
<fieldset style="border: none; padding-left: 0px; padding-top: 0px">
|
||||
<table>
|
||||
<tr>
|
||||
<td>
|
||||
<input type="submit" formaction="mail/send" value="Send Simple Email">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<input type="submit" formaction="mail/sendTemplate" value="Send Email Using Template">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<input type="submit" formaction="mail/sendAttachment" value="Send Email With Attachment">
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -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" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Send Email</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h3>${headerText}</h3>
|
||||
<form:form method="POST" modelAttribute="mailObject" >
|
||||
<fieldset>
|
||||
<div style="float:left;">
|
||||
<table cellspacing="0" width="300">
|
||||
<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_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">${messageLabel}:</label></th>
|
||||
<td><form:textarea path="text"
|
||||
rows="5" cols="50"
|
||||
id="input_text"/>
|
||||
<form:errors path="text" cssStyle="color:red;font-size:small"/>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th></th>
|
||||
<td>
|
||||
<input type="submit" value="Send">
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div style="float:left; word-wrap: break-word; margin-left: 50px; width: 400px; color: grey">
|
||||
${additionalInfo}
|
||||
</div>
|
||||
</fieldset>
|
||||
</form:form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,29 +0,0 @@
|
|||
<?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.app.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>
|
Loading…
Reference in New Issue