Could not autowire. No beans of ‘JavaMailSender’ type found.解决
javax.mail.internet.MimeMessage 解决
<!--邮箱依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId >com.sun.mail</groupId >
<artifactId >javax.mail</artifactId >
<version>1.4.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
application.properties
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.username=xxxx@qq.com
spring.mail.password=#这里是编码到请求邮箱的设置里面寻找
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
public class SendEmail implements Serializable {
/**
* 邮件接收方,可多人
*/
private String[] tos;
/**
* 邮件主题
*/
private String subject;
/**
* 邮件内容
*/
private String content;
public String[] getTos() {
return tos;
}
public void setTos(String[] tos) {
this.tos = tos;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public ToEmail() {
}
public SendEmail (String[] tos, String subject, String content) {
this.tos = tos;
this.subject = subject;
this.content = content;
}
}
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
public Boolean commonEmail(SendEmail sendEmail) {
//创建简单邮件消息
SimpleMailMessage message = new SimpleMailMessage();
//谁发的
message.setFrom(from);//from
//谁要接收
message.setTo(sendEmail.getTos());
//邮件标题
message.setSubject(sendEmail.getSubject());
//邮件内容
message.setText(sendEmail.getContent());
try {
mailSender.send(message);
return true;
} catch (MailException e) {
e.printStackTrace();
throw new ServiceException("邮箱格式错误");
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://code100.blog.csdn.net/article/details/125021906
内容来源于网络,如有侵权,请联系作者删除!