谷粒学院--申请不到阿里云短信服务,降级用QQ邮箱发送短信

在2022年,时代变了,阿里云的个人短信服务申请不到,用云市场买来的短信服务也不太会用。可以降级使用QQ邮箱来替代这个功能。

思路是:Springboot整合QQ邮箱——>在整合Swagger2进行测试

1. Springboot整合QQ邮箱

新创一个Maven工程 service-sms

结构如下

导入所需依赖

<dependencies>
    <!-- 引入swagger相关的jar -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>

    <!--邮件配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

编写application.yml文件

spring:
  mail:
    # 下面这个是QQ邮箱host , 企业邮箱 : smtp.exmail.qq.com
    host: smtp.qq.com
    # tencent mail port  这个是固定的
    port: 465
    # 你的QQ邮箱
    username: XXXXXXX@qq.com
    # 进入邮箱配置后得到的授权码
    password: XXXXXXXXXXXXXX
    test-connection: true
    properties:
      mail:
        smtp:
          ssl:
            enable: true

编写一个产生随机数字的类,我用的是谷粒学院老师那个产生随机数的类

RandomUtil

/**
 * 获取随机数
 */
public class RandomUtil {

   private static final Random random = new Random();

   private static final DecimalFormat fourdf = new DecimalFormat("0000");

   private static final DecimalFormat sixdf = new DecimalFormat("000000");

   public static String getFourBitRandom() {
      return fourdf.format(random.nextInt(10000));
   }

   public static String getSixBitRandom() {
      return sixdf.format(random.nextInt(1000000));
   }

   /**
    * 给定数组,抽取n个数据
    * @param list
    * @param n
    * @return
    */
   public static ArrayList getRandom(List list, int n) {

      Random random = new Random();

      HashMap<Object, Object> hashMap = new HashMap<Object, Object>();

      // 生成随机数字并存入HashMap
      for (int i = 0; i < list.size(); i++) {

         int number = random.nextInt(100) + 1;

         hashMap.put(number, i);
      }

      // 从HashMap导入数组
      Object[] robjs = hashMap.values().toArray();

      ArrayList r = new ArrayList();

      // 遍历数组并打印数据
      for (int i = 0; i < n; i++) {
         r.add(list.get((int) robjs[i]));
         System.out.print(list.get((int) robjs[i]) + "\t");
      }
      System.out.print("\n");
      return r;
   }
}

编写controller类  MailDemoController

@RequestMapping("/mail/")
@RestController
public class MailDemoController {

    @Autowired
    private JavaMailSender javaMailSender;

    @GetMapping("{DstEmail}")
    public String senderMail(String DstEmail) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 发件人 你的邮箱
        message.setFrom("XXXXXXXX@qq.com");
        // 接收人 接收者邮箱
        message.setTo(DstEmail);

        //邮件标题
        message.setSubject("hello");
        String code = RandomUtil.getSixBitRandom();
        //邮件内容
        message.setText("你好,我是陈冠希,我现在在洛杉矶,本次的验证码为:\t"+code+"\t"+"有效时间是5分钟,转我300块,等我回去给你300亿");
        javaMailSender.send(message);
        return "success";
    }
}

这个时候可以用Swagger来测试一下

整合一个Swagger配置类 SwaggerConfig

@Configuration
@EnableSwagger2           //开启swagger2
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }


    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("XXX", "http://XXX.com",
                        "XXXXXXXX@qq.com"))
                .build();
    }
}

在启动类上添加扫描包注解

@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class TestEmailApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestEmailApplication.class, args);
    }

}

然后运行一下测试

 

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值