官方文档地址:
https://cloud.tencent.com/document/product/382/37745
按照文档一步一步配完,就可以开发了。
发送短信接口
API文档地址:
https://cloud.tencent.com/document/product/382/38778
接口在线调试
jar包下载
https://mvnrepository.com/artifact/com.tencentcloudapi/tencentcloud-sdk-java/3.1.69
有maven的可以用maven,没有maven直接点击图中红色框内的按钮进行下载。
项目工具类
这里已验证码作为样例
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import java.io.IOException;
public class SMSUtils {
//腾讯云API密钥ID
private final static String SECRET_ID= "AKID5*************************l";
//腾讯云API密钥ID
private final static String SECRET_KEY= "pNhlt*************************58";
//腾讯云API密钥ID
private final static String URL= "sms.tencentcloudapi.com";
//腾讯云短信模板ID
private final static String TEMPLATE_ID= "6*****1";
//腾讯云应用ID
private final static String SMS_SDK_APP_ID= "14*******1";
public static void sendRegCode(String phone, String code) throws IOException {
if(StringUtils.isNotEmpty(phone)){
try{
Credential cred = new Credential(SECRET_ID, SECRET_KEY);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(URL);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(cred, "", clientProfile);
String params = "{\"PhoneNumberSet\":[\"+86"+ phone +"\"],\"TemplateID\":\""+ TEMPLATE_ID +"\",\"Sign\":\"腾讯云短信\",\"TemplateParamSet\":[\""+ code +"\"],\"SmsSdkAppid\":\""+ SMS_SDK_APP_ID +"\"}";
SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
SendSmsResponse resp = client.SendSms(req);
System.out.println(SendSmsRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}
public static void main(String [] args) {
//您的手机号码
String phone = "18111391111";
String code = "666666";
try {
SMSUtils.sendRegCode(phone,code);
} catch (IOException e) {
e.printStackTrace();
}
}
}