Java教程

SpringBoot实现QQ邮件发送

本文主要是介绍SpringBoot实现QQ邮件发送,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  1. 建项目

    1. 创建一个SpringBoot项目
  2. 改pom,导入相关依赖

    org.springframework.boot
    spring-boot-starter-parent
    2.2.2.RELEASE

    <dependencies>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--邮件发送依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
  3. 写Yml,配置application.yml

    server:
    port: 端口号

    spring:
    mail:
    #邮件发送配置
    default-encoding: UTF-8
    host: smtp.qq.com
    # 授权码
    password: 你的授权码
    # 邮件发送安全配置
    properties:
    mail:
    smtp:
    auth: true
    starttls:
    enable: true
    required: true
    # 发件人信息
    username: 发件人邮箱

  4. 主启动类EmailSignupApplication

    /**

    • @author QiuQiu&LL
    • @create 2021-08-09 2:18
    • @Description:
      */
      @SpringBootApplication
      @ComponentScan("com.qbb")
      public class EmailSignupApplication {
      public static void main(String[] args) {
      SpringApplication.run(EmailSignupApplication.class, args);
      }
      }
  5. 业务

    1. 创建Service

    /**

    • @author QiuQiu&LL
    • @create 2021-08-09 2:18
    • @Description:
      /
      public interface MailService {
      /
      *
      • 发送邮件
      • @param to 邮件收件人
      • @param subject 邮件主题
      • @param verifyCode 邮件验证码
        */
        public void sendVertifyCode(String to, String subject, String verifyCode);
        }
    1. 实现类ServiceImpl

    package com.qbb.email_signup.service.impl;

    import com.qbb.email_signup.service.MailService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.stereotype.Service;

    /**

    • @author QiuQiu&LL

    • @create 2021-08-09 2:20

    • @Description:
      */
      @Service
      public class MailServiceImpl implements MailService {

      @Value("${spring.mail.username}")
      private String from;

      @Autowired
      private JavaMailSender mailSender;

      Logger logger = LoggerFactory.getLogger(this.getClass());

      /**

      • 发送邮件
      • @param to 邮件收件人
      • @param subject 邮件主题
      • @param verifyCode 邮件验证码
        */
        @Override
        public void sendVertifyCode(String to, String subject, String verifyCode) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); //发送人
        message.setTo(to); //收件人
        message.setSubject(subject); //邮件名
        message.setText(verifyCode); //邮件内容(验证码)
        mailSender.send(message);
        logger.info("已经发送");
        }
        }
  6. 测试

    package com.qbb.email_signup;

    import com.qbb.email_signup.service.MailService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;

    /**

    • @author QiuQiu&LL

    • @create 2021-08-09 2:28

    • @Description:
      */
      @RunWith(SpringRunner.class)
      @SpringBootTest(classes = EmailSignupApplication.class)
      public class MailServiceTest {

      @Autowired
      private MailService mailService;

      @Test
      public void Test1() {
      /填你的测试信息/
      String to = "收件人邮箱";
      String title = "测试邮件";
      String context = "测试验证码";
      mailService.sendVertifyCode(to, title, context);
      }

    }

  7. 结果

这篇关于SpringBoot实现QQ邮件发送的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!