首先导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> 然后设置配置文件 我用的是qq邮箱
mail: default-encoding: UTF-8 host: smtp.qq.com username: qq邮箱号 password: 百度一下,qq邮箱设置里面哪个pop3的十六位字符串 port: 587 properties: mail: smtp: auth: true starttls: enable: true required: true 然后是后台设置 注入对象
@Resource private JavaMailSender mailSender; 设置发送邮箱
@Value("${spring.mail.username}") private String sendUser;
public void sendEmailCode(String email) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(new InternetAddress(sendUser)); //email 是接收者邮箱 helper.setTo(email); helper.setSubject("测试"); helper.setText("测试成功",true); mailSender.send(message); } 不难用 就是网上资料不全 哪个配置文件搞了很久