85 lines
2.9 KiB
Java
85 lines
2.9 KiB
Java
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;
|
|
import org.springframework.validation.Errors;
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
import javax.validation.Valid;
|
|
|
|
/**
|
|
* Created by Olga on 7/20/2016.
|
|
*/
|
|
@Controller
|
|
@RequestMapping("/mail")
|
|
public class MailController {
|
|
@Autowired
|
|
public EmailServiceImpl emailService;
|
|
|
|
@Value("${attachment.invoice}")
|
|
private String attachmentPath;
|
|
|
|
@Autowired
|
|
@Qualifier("templateSimpleMessage")
|
|
public SimpleMailMessage template;
|
|
|
|
@RequestMapping(value = {"/send", "/sendTemplate", "/sendAttachment"}, method = RequestMethod.GET)
|
|
public String createMail(Model model) {
|
|
model.addAttribute("mailObject", new MailObject());
|
|
return "mail/send";
|
|
}
|
|
|
|
@RequestMapping(value = "/send", method = RequestMethod.POST)
|
|
public String createMail(Model model,
|
|
@ModelAttribute("mailObject") @Valid MailObject mailObject,
|
|
Errors errors) {
|
|
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";
|
|
}
|
|
}
|