Spring Boot email
配置依赖
pom.xml文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
application.yml
spring: mail: host: smtp.163.com username: username password: password properties: mail: smtp: auth: true connectiontimeout: 5000 timeout: 5000 writetimeout: 5000 starttls: enable: true email: account: username
这里其实有两个注意点,因为我使用的是163邮箱,1.需要授权客户端授权密码,2. 需要勾选POP3/SMTP服务
简单示例
@Component public class SendEmail implements SendMessage{ @Autowired private JavaMailSender mailSender; @Value("${email.account}") private String account; @Override public void send(MessageForm messageForm) { SimpleMailMessage msg = new SimpleMailMessage(); msg.setFrom(account); // msg.setTo(messageForm.getReceivers().toArray(new String[0])); msg.setTo("eamil1@qq.com","email2@qq.com"); msg.setSubject(messageForm.getTitle()); msg.setText(messageForm.getInfo()); mailSender.send(msg); } }
这里我一直报553错误,原因是未设置发送者,添加msg.setFrom()即可。