腾讯【短信服务】SpringBoot代码
腾讯云【短信服务】SpringBoot代码
一、SpringBoot
导入pom.xml 依赖包:
1 2 <dependency> 3 <groupId>com.tencentcloudapigroupId> 4 <artifactId>tencentcloud-sdk-javaartifactId> 5 <version>3.1.87version> 6 dependency> 7 <dependency> 8 <groupId>com.github.qcloudsmsgroupId> 9 <artifactId>qcloudsmsartifactId> 10 <version>1.0.6version> 11 dependency>编写TengXunSMSUtils类:
1 package com.wgxxkj.util; 2 3 import com.alibaba.fastjson.JSONException; 4 import com.github.qcloudsms.SmsSingleSender; 5 import com.github.qcloudsms.SmsSingleSenderResult; 6 import com.github.qcloudsms.httpclient.HTTPException; 7 8 import java.io.IOException; 9 import java.io.Serializable; 10 import java.util.Random; 11 import java.util.regex.Matcher; 12 import java.util.regex.Pattern; 13 14 /** 15 * @Description: 16 * @Author: 小鹿 17 * @Time: 2022/3/1 14:00 18 */ 19 public class TengXunSMS { 20 21 //短信 Sdk_appId 22 private static final int appId = 1400123456; 23 24 //短信 Sdk_appKey 25 private static final String appKey = "58e5bfd0ecasdfghjklqwrtyuio"; 26 27 //短信 templateId 模板 ID 28 private static final String templateId = "1234567"; 29 30 // 根据阿里巴巴代码规范,将Pattern设置为全局常量 31 // 通过 -?[0-9]+(\\\\.[0-9]+)? 进行匹配是否为数字 32 private static Pattern pattern = Pattern.compile("-?[0-9]+(\\\\\\\\.[0-9]+)?"); 33 34 /** 35 * 发送信息 36 * 37 * @param phoneNumbers 38 * @return 39 */ 40 public static String send(String phoneNumbers) { 41 boolean number = isNumber(phoneNumbers); 42 if (number == true) { 43 int yzm = random(); 44 String num = Integer.toString(yzm); 45 SmsSingleSenderResult result = null; 46 try { 47 /* 48 这里的sms_Sign要和你的短信模板一样,如果不知道可以进官网测试一下,发条信息在手机上 49 测试地址https://console.cloud.tencent.com/api/explorer?Product=sms&Version=2021-01-11&Action=SendSms&SignVersion= 50 */ 51 String smsSign = "【XXX公众号】您的动态验证码:" + yzm + ",如非本人操作,请忽略本短信!"; 52 SmsSingleSender ssender = new SmsSingleSender(appId, appKey); 53 //国内:86+号码 54 result = ssender.send(0, "86", phoneNumbers, smsSign, "", ""); 55 if (result.result == 0) { 56 //发送成功返回验证码 57 //return num; 58 return smsSign; 59 } 60 } catch (HTTPException e) { 61 //System.out.println("HTTP响应码错误"); 62 return result.toString(); 63 } catch (JSONException e) { 64 //System.out.println("json解析错误"); 65 return result.toString(); 66 } catch (IOException e) { 67 //System.out.println(" 网络IO错误"); 68 return result.toString(); 69 } 70 return result.toString(); 71 } 72 return null; 73 } 74 75 /** 76 * 生成6位验证码 77 * 78 * @return 79 */ 80 public static int random() { 81 Random r = new Random(); 82 StringBuffer sb = new StringBuffer(); 83 int[] c = new int[6]; 84 for (int i = 0; i < 6; i++) { 85 c[i] = r.nextInt(9) + 1; 86 sb.append(c[i]); 87 } 88 return Integer.parseInt(sb.toString()); 89 } 90 91 /** 92 * 通过正则表达式判断字符串是否为:数字且首位为“1” 93 * 94 * @param str 95 * @return 96 */ 97 public static boolean isNumber(String str) { 98 //判断第一个数字是否为“1” 99 if (str.startsWith("2") == true) { 100 if (str != null) { 101 Matcher m = pattern.matcher(str); 102 m.matches(); 103 return m.matches(); 104 } 105 } 106 return false; 107 } 108 109 /** 110 * 测试 111 * 112 * @param args 113 */ 114 public static void main(String[] args) { 115 boolean number = TengXunSMS.isNumber("211111111"); 116 System.out.println(number); 117 } 118 }运行测试: