springboot send email

1. Usage scenarios

send email

2. References

2.1 https://blog.csdn.net/u011244202/article/details/54809696

Description: 2.1 copied in this article, used as a backup, has been practiced, but springboot2.0.1.RELEASE has not encountered the 535 error described in 2.1

3. Test environment

jdk8、springboot2.0.1.RELEASE

4. Use

4.1 Dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

4.2 Configuration file (application.properties)

# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.163.com
spring.mail.username=用户163邮箱
spring.mail.password=邮箱密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

Note: If you use QQ mailbox to send emails, you need to change it to the authorization code of QQ mailbox  at the spring.mail.host=smtp.qq.comsame time . QQ Mail->Settings->Account->POP3/SMTP Service: After opening the service, you will get the authorization code of QQ spring.mail.password

4.3 Code

@Autowired
    private JavaMailSender mailSender; //自动注入的Bean

    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数

    
    public void sendSimpleMail() throws Exception {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(Sender);
        message.setTo(Sender);
        message.setSubject("主题");
        message.setText("内容");
        mailSender.send(message);
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342796&siteId=291194637