commit
6c1de58262
2
pom.xml
2
pom.xml
|
@ -184,14 +184,12 @@
|
|||
<module>spring-katharsis</module>
|
||||
<module>spring-ldap</module>
|
||||
<module>spring-mockito</module>
|
||||
<module>spring-mvc-email</module>
|
||||
<module>spring-mvc-forms-jsp</module>
|
||||
<module>spring-mvc-forms-thymeleaf</module>
|
||||
<module>spring-mvc-java</module>
|
||||
<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,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,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>
|
|
@ -5,3 +5,4 @@
|
|||
- [Spring 5 and Servlet 4 – The PushBuilder](http://www.baeldung.com/spring-5-push)
|
||||
- [Servlet Redirect vs Forward](http://www.baeldung.com/servlet-redirect-forward)
|
||||
- [Apache Tiles Integration with Spring MVC](http://www.baeldung.com/spring-mvc-apache-tiles)
|
||||
- [Guide to Spring Email](http://www.baeldung.com/spring-email)
|
||||
|
|
|
@ -20,6 +20,15 @@
|
|||
<artifactId>spring-oxm</artifactId>
|
||||
<version>${spring-oxm.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
|
@ -179,7 +188,7 @@
|
|||
<jstl.version>1.2</jstl.version>
|
||||
<javax.servlet.jsp-api.version>2.3.2-b02</javax.servlet.jsp-api.version>
|
||||
<javax.servlet-api.version>4.0.0</javax.servlet-api.version>
|
||||
<hibernate-validator.version>5.4.1.Final</hibernate-validator.version>
|
||||
<hibernate-validator.version>6.0.10.Final</hibernate-validator.version>
|
||||
<deploy-path>enter-location-of-server</deploy-path>
|
||||
<fileupload.version>1.3.2</fileupload.version>
|
||||
<java.version>1.8</java.version>
|
||||
|
|
|
@ -8,6 +8,9 @@ import org.springframework.context.annotation.Configuration;
|
|||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.http.converter.feed.RssChannelHttpMessageConverter;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
|
@ -20,10 +23,11 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
|
||||
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator", "com.baeldung.spring.mail" })
|
||||
public class ApplicationConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
|
@ -60,4 +64,29 @@ public class ApplicationConfiguration implements WebMvcConfigurer {
|
|||
converters.add(new RssChannelHttpMessageConverter());
|
||||
converters.add(new JsonChannelHttpMessageConverter());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleMailMessage templateSimpleMessage() {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setText("This is the test email template for your email:\n%s\n");
|
||||
return message;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JavaMailSender getJavaMailSender() {
|
||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
||||
mailSender.setHost("smtp.gmail.com");
|
||||
mailSender.setPort(587);
|
||||
|
||||
mailSender.setUsername("my.gmail@gmail.com");
|
||||
mailSender.setPassword("password");
|
||||
|
||||
Properties props = mailSender.getJavaMailProperties();
|
||||
props.put("mail.transport.protocol", "smtp");
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
props.put("mail.debug", "true");
|
||||
|
||||
return mailSender;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package com.baeldung.spring.controllers;
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import com.baeldung.spring.mail.EmailServiceImpl;
|
||||
import com.baeldung.spring.web.dto.MailObject;
|
||||
import com.baeldung.spring.domain.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;
|
||||
|
@ -63,6 +62,11 @@ public class MailController {
|
|||
labels.put("sendAttachment", props);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public String showEmailsPage() {
|
||||
return "emails";
|
||||
}
|
||||
|
||||
@RequestMapping(value = {"/send", "/sendTemplate", "/sendAttachment"}, method = RequestMethod.GET)
|
||||
public String createMail(Model model,
|
||||
HttpServletRequest request) {
|
|
@ -1,7 +1,6 @@
|
|||
package com.baeldung.spring.web.dto;
|
||||
|
||||
import org.hibernate.validator.constraints.Email;
|
||||
package com.baeldung.spring.domain;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
|
@ -8,9 +8,10 @@ import org.springframework.mail.javamail.JavaMailSender;
|
|||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by Olga on 7/15/2016.
|
|
@ -1,3 +1,5 @@
|
|||
#this property file will have to be loaded explicitly as this is not a Spring Boot project
|
||||
|
||||
# Gmail SMTP
|
||||
spring.mail.host=smtp.gmail.com
|
||||
spring.mail.port=587
|
Loading…
Reference in New Issue