1.Java实现前序工作
1.1点击去腾讯云
1.2 如图所示操作获取短信业务
1.3创建 “签名” 与 模板 去微信公众平台注册自己的公众号或小程序)因为签名与模板需要 微信公众或者小程序的的相关内容*
签名添加
模板添加(注意我的模板内容,🉐跟我一样{},{})
1.4查看短信条数
1.5应用列表添加
*** 点击自己添加的得到关键信息
2 导入依赖 或者jar包(我创建的的是maven项目)
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.87</version>
</dependency>
<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId>
<version>1.0.6</version>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
3.短信验证的发送代码
package org.example;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.json.JSONException;
import javax.xml.ws.http.HTTPException;
import java.io.IOException;
import java.util.Random;
/**
- @Auther: 郑兴源
- @DateTime : 2021-12-23 下午 05:48
- @descriptoin:
*/
public class SendMessageUtil {
public static void main(String[] args) {
这里输入步骤1.5的appID
int appid = 140060;
//这里输入步骤1.5的appkey
String appkey = “0c099221a41cf0055a7ccfc9e9******”;
//{“这里输入手机号”,“第二个手机号”}; //手机号可以添很多。。
String[] phoneNumbers = {
“1827385****”};
//这里添你申请的模板ID,注意是模板ID不是签名ID;
int templateId = 1240;
//这里添你申请的签名,注意不是ID,是签名,中文。"
String smsSign = “HY信息工作室”;
//随机数字6位
Random random =new Random();
String randNum = “”;
for (int i = 0; i < 6; i++) {
randNum += random.nextInt(10);
}
//存验证码在服务器—可应用与servlet中
// HttpSession session = req.getSession();
// session.setAttribute(“trueyzm”, randNum);
//以秒为单位,即在没有活动30分钟后,session将失效
//session.setMaxInactiveInterval(3060);
// System.out.println(“yzm->:” + result);
try {
//第一个参数传递{1}位置想要的内容,第二个传递{2}的内容,以此类推。
String[] params = {
randNum,“10”};
SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
SmsSingleSenderResult result = ssender.sendWithParam(“86”, phoneNumbers[0],
templateId, params, smsSign, “”, “”);
System.out.println(result);
} catch (HTTPException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (com.github.qcloudsms.httpclient.HTTPException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59