使用Spring Boot发送电子邮件

时间:2017-06-26 16:42:46

标签: java spring-boot

我有一个带有From / Tp输入字段的Form和TextArea as Box。我想使用Spring Boot发送电子邮件。如何使用Spring Boot发送电子邮件?

感谢回答。

1 个答案:

答案 0 :(得分:1)

现在工作正常

那是我的控制器

@Controller
@RequestMapping("/sendmail")
public class MailController {
    @Autowired
    private MailService mailService;


    @RequestMapping(method = RequestMethod.POST)
    private String sendMail(HttpServletRequest request) {
         String covere = request.getParameter("covere");

        try {
            mailService.sendMail(covere);
            return "apply";
        } catch (MailException e) {
            e.printStackTrace();
        }
        return "apply";
    }
}

那是我的服务

@Component
public class MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    public void sendMail(String covere) throws MailException{

        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setTo("yottiallipierre@gmail.com");
        mail.setFrom("yottiallipierre@gmail.com");
        mail.setSubject("Test");
        mail.setText(covere);
        javaMailSender.send(mail);
    }

}

那是我的属性档案

spring.mail.host = smtp.gmail.com
spring.mail.port= 465
spring.mail.username = yottiallipierre@gmail.com
spring.mail.password = losangeles4004*
send.from.email= yottiallipierre@gmail.com
spring.mail.properties.mail.smtp.auth = true;
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable = true
spring.mail.properties.mail.socketFactory.port=587
spring.mail.properties.mail.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.socketFactory.fallback=false