目录
🌮1.获取qq授权码
🫓2.引入依赖
🧈3.配置mail信息
🥞4.创建实现类
🥖5.测试
1.获取qq授权码
点击开启服务,发送信息获取授权码
2.引入依赖
<!--邮件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
3.配置mail信息
spring:mail:host: smtp.qq.com #发送邮箱服务器username: 3100602334@qq.com #发送邮箱的地址password: drtasyovwpqpdhfc #客户端授权码default-encoding: utf-8properties.mail.stmp.starttls.enbale: trueproperties.mail.stmp.starttls.require: trueproperties.mail.stmp.ssl.enable: true
4.创建实现类
@Slf4j
@Service
public class MailServiceImpl implements MailService {/*** springboot的发送邮件的对象,可读取配置文件*/@Autowiredprivate JavaMailSender mailSender;/*** 发件人,从配置文件读取*/@Value("${spring.mail.username}")private String from;/*** 发送邮件* @param to:收件人* @param subject:主题* @param content:内容*/@Overridepublic void sendMail(String to, String subject, String content) {//1.创建邮箱消息对象SimpleMailMessage message = new SimpleMailMessage();//2.配置邮件发送人message.setFrom(from);//3.邮件接收人message.setTo(to);//4.邮件主题message.setSubject(subject);//5.邮件内容message.setText(content);//6.发送邮件mailSender.send(message);log.info("邮件发送成功");}
}
5.测试
@Autowiredprivate MailService mailService;@Testpublic void test3(){//收件人的邮箱String to="3100@qq.com";//邮件的主体String subject="欢迎查收小张的邮件";//邮件的内容String content="未来的日子里笑口常开哦~";mailService.sendMail(to,subject,content);}