短信发送验证码及邮件发送验证码

发送短信验证码

阿里云发送验证码

 public Integer sendTelCode(String tel) {
        String url = "https://dfsns.market.alicloudapi.com/data/send_sms";
        String appcode = "a3198282fbdf443d97aa9f3cfbe1232e";
        int code = RandomUtil.randomInt(1000,10000);
        emailMap.put(tel,code);
        String result = HttpRequest.post(url)
                .header("Authorization","APPCODE "+appcode)
                .body("content=code:"+code+"&template_id=TPL_0000&phone_number="+tel)
                .execute().body();

         JSONObject object = JSONUtil.parseObj(result);
         if (!object.get("status").equals("OK")){
             log.error("发送验证码错误:{}",object.get("reason"));
             throw new BizException(404,"发送验证码错误");
         }
         return 0;
     }

在这里插入图片描述

发送邮件功能

pom.xml 引入hutool和javax.mail

 <dependency>
	 <groupId>cn.hutool</groupId>
     <artifactId>hutool-all</artifactId>
  </dependency>
 <dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
 </dependency>

2.书写代码

首先你的邮箱要开启SMTP服务.
书写代码:

     public Integer sendCode(String email) {
        //MailAccount 邮件账户对象
         MailAccount account = new MailAccount();
         account.setHost("smtp.aliyun.com");//阿里云的邮箱服务器地址
         account.setPort(25); //邮件服务器端口,默认25
         account.setAuth(true);
         account.setUser("3813@aliyun.com");
         account.setFrom("3813@aliyun.com");
         account.setPass("这里是自己的邮箱密码");
         int code = RandomUtil.randomInt(1000,10000);//RandomUtil:随机工具类
         emailMap.put(email,code);
         //MailUtil:邮件的工具类
         MailUtil.send(account,email,"WMS验证码","验证码:"+code,false);
         return 0;
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值