Implemented changes according to the latest comments in my pull request.
Now we can send email from the web. Access: http://localhost:8080
This commit is contained in:
parent
6a599b9406
commit
095fe5625a
6
spring-mvc-email/readme.txt
Normal file
6
spring-mvc-email/readme.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
You can send test email in several ways:
|
||||||
|
First of all, you should set application.properties properly.
|
||||||
|
1. Just build the project and the test email for order confirmation will be sent on Spring Boot Start.
|
||||||
|
2. You can use the web form and test all the cases of sending email in your browser.
|
||||||
|
Just start the application then type http://localhost:8080 and follow the web flow.
|
||||||
|
You can send simple email, email with template, email with attachment from the web.
|
@ -27,8 +27,7 @@ public class Application {
|
|||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void postConstruct() {
|
public void postConstruct() {
|
||||||
//emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message");
|
Order order = new Order("user_email_address", "First Name", "Last Name");
|
||||||
Order order = new Order("reva.olga@gmail.com", "First Name", "Last Name");
|
|
||||||
orderManager.placeOrder(order);
|
orderManager.placeOrder(order);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ public class AppConfig extends WebMvcConfigurerAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SimpleMailMessage templateMessage() {
|
public SimpleMailMessage templateOrderMessage() {
|
||||||
SimpleMailMessage message = new SimpleMailMessage();
|
SimpleMailMessage message = new SimpleMailMessage();
|
||||||
message.setText("Dear %s %s, \nthank you for placing order.\n" +
|
message.setText("Dear %s %s, \nthank you for placing order.\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
@ -53,44 +53,10 @@ public class AppConfig extends WebMvcConfigurerAdapter {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Gmail */
|
|
||||||
/*@Bean
|
|
||||||
public JavaMailSenderImpl mailSender() {
|
|
||||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
|
||||||
mailSender.setHost("smtp.gmail.com");
|
|
||||||
mailSender.setPort(587);
|
|
||||||
mailSender.setUsername("username");
|
|
||||||
mailSender.setPassword("password");
|
|
||||||
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;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*Amazon SES
|
|
||||||
@Bean
|
@Bean
|
||||||
public JavaMailSenderImpl mailSender() {
|
public SimpleMailMessage templateSimpleMessage() {
|
||||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
SimpleMailMessage message = new SimpleMailMessage();
|
||||||
mailSender.setHost("email-smtp.us-west-2.amazonaws.com");
|
message.setText("This is the test email template for your email:\n%s\n");
|
||||||
mailSender.setUsername("username");
|
return message;
|
||||||
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;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ package com.baeldung.spring.controllers;
|
|||||||
import com.baeldung.spring.mail.EmailServiceImpl;
|
import com.baeldung.spring.mail.EmailServiceImpl;
|
||||||
import com.baeldung.spring.web.dto.MailObject;
|
import com.baeldung.spring.web.dto.MailObject;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.mail.SimpleMailMessage;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
@ -22,6 +24,13 @@ public class MailController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
public EmailServiceImpl emailService;
|
public EmailServiceImpl emailService;
|
||||||
|
|
||||||
|
@Value("${attachment.invoice}")
|
||||||
|
private String attachmentPath;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("templateSimpleMessage")
|
||||||
|
public SimpleMailMessage template;
|
||||||
|
|
||||||
@RequestMapping(value = "/send", method = RequestMethod.GET)
|
@RequestMapping(value = "/send", method = RequestMethod.GET)
|
||||||
public String createMail(Model model) {
|
public String createMail(Model model) {
|
||||||
model.addAttribute("mailObject", new MailObject());
|
model.addAttribute("mailObject", new MailObject());
|
||||||
@ -32,7 +41,43 @@ 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) {
|
||||||
emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message");
|
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";
|
return "redirect:/home";
|
||||||
}
|
}
|
||||||
|
@ -69,46 +69,4 @@ public class EmailServiceImpl implements EmailService {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public void sendMail(MimeMessage message) {
|
|
||||||
try {
|
|
||||||
emailSender.send(message);
|
|
||||||
} catch (MailException exception) {
|
|
||||||
exception.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public MimeMessage createMessageWithAttachment(MailObject mailObject) {
|
|
||||||
MimeMessage message = emailSender.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;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*@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;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.baeldung.spring.mail;
|
package com.baeldung.spring.mail;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.mail.SimpleMailMessage;
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -16,7 +17,8 @@ public class OrderManager {
|
|||||||
@Value("${attachment.invoice}")
|
@Value("${attachment.invoice}")
|
||||||
private String invoiceAttachmentPath;
|
private String invoiceAttachmentPath;
|
||||||
|
|
||||||
@Autowired
|
@Autowired()
|
||||||
|
@Qualifier("templateOrderMessage")
|
||||||
public SimpleMailMessage template;
|
public SimpleMailMessage template;
|
||||||
|
|
||||||
public void placeOrder(Order order) {
|
public void placeOrder(Order order) {
|
||||||
|
@ -9,10 +9,6 @@ import javax.validation.constraints.Size;
|
|||||||
* Created by Olga on 7/20/2016.
|
* Created by Olga on 7/20/2016.
|
||||||
*/
|
*/
|
||||||
public class MailObject {
|
public class MailObject {
|
||||||
@Email
|
|
||||||
@NotNull
|
|
||||||
@Size(min = 1, message = "Please, specify your email address")
|
|
||||||
private String from;
|
|
||||||
@Email
|
@Email
|
||||||
@NotNull
|
@NotNull
|
||||||
@Size(min = 1, message = "Please, set an email address to send the message to it")
|
@Size(min = 1, message = "Please, set an email address to send the message to it")
|
||||||
@ -20,14 +16,6 @@ public class MailObject {
|
|||||||
private String subject;
|
private String subject;
|
||||||
private String text;
|
private String text;
|
||||||
|
|
||||||
public String getFrom() {
|
|
||||||
return from;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFrom(String from) {
|
|
||||||
this.from = from;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTo() {
|
public String getTo() {
|
||||||
return to;
|
return to;
|
||||||
}
|
}
|
||||||
|
@ -18,3 +18,4 @@ spring.mail.properties.mail.smtp.starttls.enable=true
|
|||||||
|
|
||||||
# path to attachment file
|
# path to attachment file
|
||||||
attachment.invoice=path_to_file
|
attachment.invoice=path_to_file
|
||||||
|
#attachment.invoice=c:/invoice.jpg
|
@ -1,23 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xmlns:context="http://www.springframework.org/schema/context"
|
|
||||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
|
||||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
|
||||||
http://www.springframework.org/schema/context
|
|
||||||
http://www.springframework.org/schema/context/spring-context.xsd
|
|
||||||
http://www.springframework.org/schema/mvc
|
|
||||||
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
|
|
||||||
>
|
|
||||||
<context:component-scan base-package="com.baeldung.spring"/>
|
|
||||||
<mvc:annotation-driven/>
|
|
||||||
|
|
||||||
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
|
|
||||||
<property name="order" value="0"/>
|
|
||||||
<property name="prefix" value="/WEB-INF/views/"/>
|
|
||||||
<property name="suffix" value=".jsp"/>
|
|
||||||
<property name="cache" value="false"/>
|
|
||||||
<property name="viewClass" value="JstlView"/>
|
|
||||||
</bean>
|
|
||||||
</beans>
|
|
@ -24,13 +24,6 @@
|
|||||||
<form:errors path="to" cssStyle="color:red;font-size:small"/>
|
<form:errors path="to" cssStyle="color:red;font-size:small"/>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</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>
|
<tr>
|
||||||
<th><label for="input_subject">Subject</label></th>
|
<th><label for="input_subject">Subject</label></th>
|
||||||
<td><form:input path="subject" id="input_subject"/>
|
<td><form:input path="subject" id="input_subject"/>
|
||||||
@ -48,8 +41,11 @@
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
<th></th>
|
||||||
<td>
|
<td>
|
||||||
<form:button>Send</form:button>
|
<input type="submit" formaction="send" value="Send">
|
||||||
|
<input type="submit" formaction="sendTemplate" value="Send Using Template">
|
||||||
|
<input type="submit" formaction="sendAttachment" value="Send With Attachment">
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
@ -26,16 +26,4 @@
|
|||||||
<url-pattern>/</url-pattern>
|
<url-pattern>/</url-pattern>
|
||||||
</servlet-mapping>
|
</servlet-mapping>
|
||||||
|
|
||||||
<!--<listener>
|
|
||||||
<listener-class>
|
|
||||||
org.springframework.web.context.ContextLoaderListener
|
|
||||||
</listener-class>
|
|
||||||
</listener>
|
|
||||||
|
|
||||||
<context-param>
|
|
||||||
<param-name>contextConfigLocation</param-name>
|
|
||||||
<param-value>
|
|
||||||
/WEB-INF/simpleweb-servlet.xml
|
|
||||||
</param-value>
|
|
||||||
</context-param>-->
|
|
||||||
</web-app>
|
</web-app>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user