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 1f536e3297..768a0f8e7b 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 @@ -13,7 +13,12 @@ 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; /** * Created by Olga on 7/20/2016. @@ -31,8 +36,50 @@ public class MailController { @Qualifier("templateSimpleMessage") public SimpleMailMessage template; + private static final Map> labels; + + static { + labels = new HashMap<>(); + + //Simple email + Map 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:
" + + "This is the test email template for your email:
'Template Parameter'
" + ); + 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) { + public String createMail(Model model, + HttpServletRequest request) { + String action = request.getRequestURL().substring( + request.getRequestURL().lastIndexOf("/") + 1 + ); + Map props = labels.get(action); + Set keys = props.keySet(); + Iterator iterator = keys.iterator(); + while (iterator.hasNext()) { + String key = iterator.next(); + model.addAttribute(key, props.get(key)); + } + model.addAttribute("mailObject", new MailObject()); return "mail/send"; } diff --git a/spring-mvc-email/src/main/resources/application.properties b/spring-mvc-email/src/main/resources/application.properties index ba0608c3af..61a42050e5 100644 --- a/spring-mvc-email/src/main/resources/application.properties +++ b/spring-mvc-email/src/main/resources/application.properties @@ -17,5 +17,4 @@ 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=c:/invoice.jpg \ No newline at end of file +attachment.invoice=path_to_file \ 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 294cb2c49c..d27aa09d9a 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 @@ -13,40 +13,44 @@
-

Create Email

- +

${headerText}

+
- - - - - - - - - - - - - - - - - -
- Enter email address
- -
- Enter the subject
- -
- Enter message text
- -
- -
+
+ + + + + + + + + + + + + + + + + +
+ Enter email address
+ +
+ Enter the subject
+ +
+ +
+ +
+
+
+ ${additionalInfo} +