Java 之 Spring Boot 发送邮箱(解决依赖找不到异常)

Posted 蓝盒子itbluebox

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 之 Spring Boot 发送邮箱(解决依赖找不到异常)相关的知识,希望对你有一定的参考价值。

Could not autowire. No beans of ‘JavaMailSender’ type found.解决
javax.mail.internet.MimeMessage 解决

1、引入依赖(注意版本号)pom.xml

	   <!--邮箱依赖-->
        <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>

2、设置配置文件

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



3、编写实体类


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;
    


4、注入并发送邮件

    @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("邮箱格式错误");
        
    

以上是关于Java 之 Spring Boot 发送邮箱(解决依赖找不到异常)的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot 速成(10) -个人邮箱/企业邮箱发送邮件

spring boot 发送邮箱

Spring中使用 网易邮箱 或 qq邮箱 发送信息

springboot发送邮件

SpringBoot中快速实现邮箱发送

IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理