spring boot整合mail

1.添加依賴spring

    </dependency>
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2.增長配置api

spring.mail.default-encoding=utf-8
spring.mail.host=smtp.partner.outlook.cn
spring.mail.port=587
spring.mail.username=xxxx@xxxx.com
spring.mail.password=123456
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.timout=25000
spring.mail.properties.mail.smtp.socketFactory.fallback=false
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.protocol=smtps
spring.mail.properties.mail.smtp.ssl.trust=smtp.partner.outlook.cn

3.編寫測試代碼app

@Controller
@RequestMapping("/api")
public class SendMailController {

    @Autowired
    private MailSender mailSender;

    private SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

    @RequestMapping(value = "/sendmsg", method = RequestMethod.GET)
    @ResponseBody
    public String sendMessage(){

        simpleMailMessage.setSubject("~-Test-~");
        simpleMailMessage.setText("test");
        simpleMailMessage.setFrom("嘻嘻嘻");
        simpleMailMessage.setTo("嘻嘻嘻");

        mailSender.send(simpleMailMessage);

        return "Mail Sent";

    }

    @RequestMapping(value = "/hello" , method = RequestMethod.GET)
    @ResponseBody
    public String getHello(){
        return "Hello";
    }

}
相關文章
相關標籤/搜索