Spring Boot2.x集成了mail模块,在dependencies引入这个
spring: mail: username: xxxxx@qq.com password: xxxxxxxx host: smtp.qq.com port: 465 properties: mail: transport: protocol: smtp smtp: socketFactory: class: javax.net.ssl.SSLSocketFactory port: ${spring.mail.port} auth: true starttls: enable: true required: true
spring.mail.password不是QQ密码,登录mail.qq.com,前往设置 - 账户 -POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务开启POP3/SMTP服务获取一个16个字符的密码
发送邮件@Autowiredprivate JavaMailSender mailSender;@Autowiredprivate MailProperties mailProperties;@Overrideprotected void send(String receiver, String title, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(mailProperties.getUsername()); message.setTo(receiver); message.setSubject(title); message.setText(content); mailSender.send(message);}