diff --git a/spring-mvc-email/readme.txt b/spring-mvc-email/readme.txt new file mode 100644 index 0000000000..24ff5438d7 --- /dev/null +++ b/spring-mvc-email/readme.txt @@ -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. \ No newline at end of file diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java index 769658fdde..7d92c2964a 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/Application.java @@ -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); } } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java index 0c1662a36c..4fb684c2b6 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/app/config/AppConfig.java @@ -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; - }*/ } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java index 79152797ff..832a60ba5d 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/controllers/MailController.java @@ -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"; } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java index a6bcb00943..8ecde598f9 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -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; - }*/ } diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java index 23f6184c8a..5dfe25f20f 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/mail/OrderManager.java @@ -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) { diff --git a/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java b/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java index 3369ccd9f6..9623ff5d78 100644 --- a/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java +++ b/spring-mvc-email/src/main/java/com/baeldung/spring/web/dto/MailObject.java @@ -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; } diff --git a/spring-mvc-email/src/main/resources/application.properties b/spring-mvc-email/src/main/resources/application.properties index 61a42050e5..ba0608c3af 100644 --- a/spring-mvc-email/src/main/resources/application.properties +++ b/spring-mvc-email/src/main/resources/application.properties @@ -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 \ No newline at end of file +attachment.invoice=path_to_file +#attachment.invoice=c:/invoice.jpg \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml b/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml deleted file mode 100644 index 5a3bb2377a..0000000000 --- a/spring-mvc-email/src/main/webapp/WEB-INF/simpleweb-servlet.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp index 9e9cbc1ff7..1307fc2baf 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp +++ b/spring-mvc-email/src/main/webapp/WEB-INF/views/mail/send.jsp @@ -24,13 +24,6 @@ - - - - Enter your email address
- - - @@ -48,8 +41,11 @@ + - Send + + + diff --git a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml index cbc1bee20a..4cd41216d9 100644 --- a/spring-mvc-email/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-email/src/main/webapp/WEB-INF/web.xml @@ -26,16 +26,4 @@ / -