• Spring Boot Mail通过QQ邮箱发送邮件


    本文将介绍如何在Spring Boot工程完成QQ邮箱配置,实现邮件发送功能。

    一、在pom文件中添加依赖

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

    二、在application.yml文件中添加配置

    spring:
      mail:
        username: xxxxx@qq.com
        password: xxxxxxxx
        host: smtp.qq.com
        port: 465
        properties:
          mail:
            transport:
              protocol: smtp
            smtp:
              socketFactory:
                class: javax.net.ssl.SSLSocketFactory
              port: ${spring.mail.port}
              auth: true
              starttls:
                enable: true
                required: true
    
    

    将配置中的邮箱换成自己的QQ邮箱,密码使用QQ邮箱生成的授权码。接下来介绍如何生成授权码,首先登陆QQ邮箱,进入设置界面

    20200424225205

    点击账户

    20200424225708

    找到POP3/IMAP/SMPTP等配置,要启用POP3/SMTP和IMAP/SMTP两个服务,然后再点击生成授权码即可
    20200424225750

    拿到授权码之后,放在上述配置中的密码

    三、发送邮件

    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private MailProperties mailProperties;
    
    @Override
    protected void send(String receiver, String title, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailProperties.getUsername());
        message.setTo(receiver);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);
    }
    

    通过上述代码即可完成邮件发送功能,同理其他邮箱配置类似。

  • 相关阅读:
    [LeetCode] Add and Search Word
    [LintCode] Delete Digits
    [LintCode] Number of Airplanes in the Sky
    [LintCode] Subarray Sum Closest
    [LeetCode] Course Schedule II
    [LeetCode] Minimum Size Subarray Sum
    [LeetCode] Implement Trie (Prefix Tree)
    [Leetcode] Course Schedule
    [hihoCoder] 博弈游戏·Nim游戏
    [hihoCoder] #1055 : 刷油漆
  • 原文地址:https://www.cnblogs.com/thisismartin/p/12770807.html
Copyright © 2020-2023  润新知