|
| 1 | +package com.baeldung.spring.controller; |
| 2 | + |
| 3 | +import com.baeldung.spring.mail.EmailServiceImpl; |
| 4 | +import com.baeldung.spring.domain.MailObject; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.mail.SimpleMailMessage; |
| 8 | +import org.springframework.stereotype.Controller; |
| 9 | +import org.springframework.ui.Model; |
| 10 | +import org.springframework.validation.Errors; |
| 11 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 12 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 14 | + |
| 15 | +import javax.servlet.http.HttpServletRequest; |
| 16 | +import javax.validation.Valid; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +@Controller |
| 23 | +@RequestMapping("/mail") |
| 24 | +public class MailController { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + public EmailServiceImpl emailService; |
| 28 | + |
| 29 | + @Value("${attachment.invoice}") |
| 30 | + private String attachmentPath; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + public SimpleMailMessage template; |
| 34 | + |
| 35 | + private static final Map<String, Map<String, String>> labels; |
| 36 | + |
| 37 | + static { |
| 38 | + labels = new HashMap<>(); |
| 39 | + |
| 40 | + //Simple email |
| 41 | + Map<String, String> props = new HashMap<>(); |
| 42 | + props.put("headerText", "Send Simple Email"); |
| 43 | + props.put("messageLabel", "Message"); |
| 44 | + props.put("additionalInfo", ""); |
| 45 | + labels.put("send", props); |
| 46 | + |
| 47 | + //Email with template |
| 48 | + props = new HashMap<>(); |
| 49 | + props.put("headerText", "Send Email Using Template"); |
| 50 | + props.put("messageLabel", "Template Parameter"); |
| 51 | + props.put("additionalInfo", |
| 52 | + "The parameter value will be added to the following message template:<br>" + |
| 53 | + "<b>This is the test email template for your email:<br>'Template Parameter'</b>" |
| 54 | + ); |
| 55 | + labels.put("sendTemplate", props); |
| 56 | + |
| 57 | + //Email with attachment |
| 58 | + props = new HashMap<>(); |
| 59 | + props.put("headerText", "Send Email With Attachment"); |
| 60 | + props.put("messageLabel", "Message"); |
| 61 | + 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."); |
| 62 | + labels.put("sendAttachment", props); |
| 63 | + } |
| 64 | + |
| 65 | + @RequestMapping(method = RequestMethod.GET) |
| 66 | + public String showEmailsPage() { |
| 67 | + return "emails"; |
| 68 | + } |
| 69 | + |
| 70 | + @RequestMapping(value = {"/send", "/sendTemplate", "/sendAttachment"}, method = RequestMethod.GET) |
| 71 | + public String createMail(Model model, |
| 72 | + HttpServletRequest request) { |
| 73 | + String action = request.getRequestURL().substring( |
| 74 | + request.getRequestURL().lastIndexOf("/") + 1 |
| 75 | + ); |
| 76 | + Map<String, String> props = labels.get(action); |
| 77 | + Set<String> keys = props.keySet(); |
| 78 | + Iterator<String> iterator = keys.iterator(); |
| 79 | + while (iterator.hasNext()) { |
| 80 | + String key = iterator.next(); |
| 81 | + model.addAttribute(key, props.get(key)); |
| 82 | + } |
| 83 | + |
| 84 | + model.addAttribute("mailObject", new MailObject()); |
| 85 | + return "mail/send"; |
| 86 | + } |
| 87 | + |
| 88 | + @RequestMapping(value = "/send", method = RequestMethod.POST) |
| 89 | + public String createMail(Model model, |
| 90 | + @ModelAttribute("mailObject") @Valid MailObject mailObject, |
| 91 | + Errors errors) { |
| 92 | + if (errors.hasErrors()) { |
| 93 | + return "mail/send"; |
| 94 | + } |
| 95 | + emailService.sendSimpleMessage(mailObject.getTo(), |
| 96 | + mailObject.getSubject(), mailObject.getText()); |
| 97 | + |
| 98 | + return "redirect:/home"; |
| 99 | + } |
| 100 | + |
| 101 | + @RequestMapping(value = "/sendTemplate", method = RequestMethod.POST) |
| 102 | + public String createMailWithTemplate(Model model, |
| 103 | + @ModelAttribute("mailObject") @Valid MailObject mailObject, |
| 104 | + Errors errors) { |
| 105 | + if (errors.hasErrors()) { |
| 106 | + return "mail/send"; |
| 107 | + } |
| 108 | + emailService.sendSimpleMessageUsingTemplate(mailObject.getTo(), |
| 109 | + mailObject.getSubject(), |
| 110 | + template, |
| 111 | + mailObject.getText()); |
| 112 | + |
| 113 | + return "redirect:/home"; |
| 114 | + } |
| 115 | + |
| 116 | + @RequestMapping(value = "/sendAttachment", method = RequestMethod.POST) |
| 117 | + public String createMailWithAttachment(Model model, |
| 118 | + @ModelAttribute("mailObject") @Valid MailObject mailObject, |
| 119 | + Errors errors) { |
| 120 | + if (errors.hasErrors()) { |
| 121 | + return "mail/send"; |
| 122 | + } |
| 123 | + emailService.sendMessageWithAttachment( |
| 124 | + mailObject.getTo(), |
| 125 | + mailObject.getSubject(), |
| 126 | + mailObject.getText(), |
| 127 | + attachmentPath |
| 128 | + ); |
| 129 | + |
| 130 | + return "redirect:/home"; |
| 131 | + } |
| 132 | +} |
0 commit comments