大家好,我是小铁,一个酷比的程序员
大家先来看一下报错
Description:
Field javaMailSender in com.gzh.service.impl.IMailServiceImpl required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
The injection point has the following annotations:
@org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.
这个的意思就是说,在我们启动springboot项目的时候,springboot自动注入JavaMailSender这个接口的时候,发生了注入失败的情况
请耐心看完,或许对你有一些帮助
一、我们先看pom.xml文件,首先我们应该添加这个发送邮件的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二、这个是发送邮件的代码,这个没什么说的
@Override
public void sendSimpleMail(String to, String subject, String content) {
//创建SimpleMailMessage对象
SimpleMailMessage message = new SimpleMailMessage();
//邮件发送人
message.setFrom(from);
//邮件接收人
message.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容
message.setText(content);
//发送邮件
javaMailSender.send(message);
}
三、配置文件(已经做了处理了)
spring:
mail:
host: smtp.qq.com #我用的是qq,用其他的也是一样
username: xxxxxxx@qq.com #这个是qq邮箱
password: xxxxxxxxx #注意这个不是密码,是客户端授权码
default-encoding: utf-8 #这个是字符集,咱们用utf-8就可以了
针对以上的错误,咱们检查这些基本就可以解决了。
如果,代码还是报错,可以私信小铁。把报错截图发一下就可以
追加
根据好多同学的反馈,我总结一下问题。我发现很多小伙伴都是有一个共同的问题
eg:我来给大家举例一下
请大家要注意看这个层级关系。一级 二级 三级 mail是属于spring下面的。
可以看一下这张照片。这个明显就是层级的问题,他的mail 就属于logging 下面了。 请自己检查一下。我也收集一下大家反馈的问题。方便我后续处理。