java对接腾讯云短信发送
创建签名

创建正文模板

找相关信息 SDK AppID 和 App Key

找到API密钥 记录SecretId 和 SecretKey

创项目导依赖
1
2
3
4
5
6<!--腾讯短信依赖-->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>4.0.11</version>
</dependency>创建java类
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
36package com.suyi.springcloud.test;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
public class SendCode {
public static void main(String[] args) {
// secretId,secretKey
Credential cred = new Credential("secretId",
"secretKey");
// 实例化要请求产品(以cvm为例)的client对象
ClientProfile clientProfile = new ClientProfile();
clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
SmsClient smsClient = new SmsClient(cred, "ap-chongqing");//第二个ap-chongqing 填产品所在的区
SendSmsRequest sendSmsRequest = new SendSmsRequest();
sendSmsRequest.setSmsSdkAppid("SDK AppID");//SDK AppID
String[] phones={"+86手机号"}; //发送短信的目标手机号,可填多个。
sendSmsRequest.setPhoneNumberSet(phones);
sendSmsRequest.setTemplateID("模版id"); //模版id
String [] templateParam={"452256"};//模版参数,从前往后对应的是模版的{1}、{2}等
sendSmsRequest.setTemplateParamSet(templateParam);
sendSmsRequest.setSign("飞驰的苏弋"); //签名内容,不是填签名id节
try {
SendSmsResponse sendSmsResponse= smsClient.SendSms(sendSmsRequest); //发送短信
System.out.println(sendSmsResponse.toString());
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
}
}
方法二:
1 | package com.suyi.springcloud.test; |