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:
oreva 2016-09-13 09:35:23 +03:00
parent 6a599b9406
commit 095fe5625a
11 changed files with 67 additions and 141 deletions

View 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.

View File

@ -27,8 +27,7 @@ public class Application {
@PostConstruct
public void postConstruct() {
//emailService.sendSimpleMessage("to@gmail.com", "Test Subject", "Test Message");
Order order = new Order("reva.olga@gmail.com", "First Name", "Last Name");
Order order = new Order("user_email_address", "First Name", "Last Name");
orderManager.placeOrder(order);
}
}

View File

@ -44,7 +44,7 @@ public class AppConfig extends WebMvcConfigurerAdapter {
}
@Bean
public SimpleMailMessage templateMessage() {
public SimpleMailMessage templateOrderMessage() {
SimpleMailMessage message = new SimpleMailMessage();
message.setText("Dear %s %s, \nthank you for placing order.\n" +
"\n" +
@ -53,44 +53,10 @@ public class AppConfig extends WebMvcConfigurerAdapter {
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
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;
public SimpleMailMessage templateSimpleMessage() {
SimpleMailMessage message = new SimpleMailMessage();
message.setText("This is the test email template for your email:\n%s\n");
return message;
}
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

@ -3,6 +3,8 @@ 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;
@ -22,6 +24,13 @@ public class MailController {
@Autowired
public EmailServiceImpl emailService;
@Value("${attachment.invoice}")
private String attachmentPath;
@Autowired
@Qualifier("templateSimpleMessage")
public SimpleMailMessage template;
@RequestMapping(value = "/send", method = RequestMethod.GET)
public String createMail(Model model) {
model.addAttribute("mailObject", new MailObject());
@ -32,7 +41,43 @@ public class MailController {
public String createMail(Model model,
@ModelAttribute("mailObject") @Valid MailObject mailObject,
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";
}

View File

@ -69,46 +69,4 @@ public class EmailServiceImpl implements EmailService {
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;
}*/
}

View File

@ -1,6 +1,7 @@
package com.baeldung.spring.mail;
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.Component;
@ -16,7 +17,8 @@ public class OrderManager {
@Value("${attachment.invoice}")
private String invoiceAttachmentPath;
@Autowired
@Autowired()
@Qualifier("templateOrderMessage")
public SimpleMailMessage template;
public void placeOrder(Order order) {

View File

@ -9,10 +9,6 @@ import javax.validation.constraints.Size;
* Created by Olga on 7/20/2016.
*/
public class MailObject {
@Email
@NotNull
@Size(min = 1, message = "Please, specify your email address")
private String from;
@Email
@NotNull
@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 text;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}

View File

@ -17,4 +17,5 @@ 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
attachment.invoice=path_to_file
#attachment.invoice=c:/invoice.jpg

View File

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

View File

@ -24,13 +24,6 @@
<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"/>
@ -48,8 +41,11 @@
</td>
</tr>
<tr>
<th></th>
<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>
</tr>
</table>

View File

@ -26,16 +26,4 @@
<url-pattern>/</url-pattern>
</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>