发送邮件,QQ邮箱"/>
SpringBoot发送邮件,QQ邮箱
一.网页版QQ邮箱—>设置—>账户—>选择开启POP3/SMTP 服务【如果已经开启,就关闭再开启就可以获取授权码了】
二.编写写application.yml 导入对应的jar包
spring:mail:#smtp服务主机 qq邮箱则为smtp.qqhost: smtp.qq#服务协议protocol: smtp# 编码集default-encoding: UTF-8#发送邮件的账户username: xxxxxxxxxx0@qq#相对应的授权码password: xxxxxxxxxxxxxxtest-connection: trueproperties:mail:smtp:auth: truestarttls:enable: truerequired: true
pom.xml里面导包
<!--email包--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
三.ServiceImpl实现类里面注入JavaMailSender
@Autowiredprivate JavaMailSender mailSender;@Overridepublic Result sendEmail() {//创建简单邮件消息SimpleMailMessage message = new SimpleMailMessage();//谁发的,发件人的邮箱,也就是你的邮箱message.setFrom("xxxxxx@qq");//谁要接收,这里面可以写数组,可以一次发送message.setTo("xxxxxx@qq");//邮件标题-自定义message.setSubject("你的注册验证码");//邮件内容-自定义message.setText("这里是内容哈哈哈哈");try {mailSender.send(message);return new Result(true, "发送普通邮件成功");} catch (MailException e) {e.printStackTrace();return new Result(true, "发送普通邮件成功");}}
五.测试
更多推荐
SpringBoot发送邮件,QQ邮箱
发布评论