[Java] 纯文本查看 复制代码 package example.service;
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;
import example.Systems.Contros;
import org.springframework.stereotype.Service;
@Service
public class IndexService {
private static String code;
public String upDuanXin(String number){
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
Credential cred = new Credential(Contros.secretId,Contros.secretKey);
// 实例化要请求产品(以cvm为例)的client对象
ClientProfile clientProfile = new ClientProfile();
clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
SmsClient smsClient = new SmsClient(cred, "ap-chongqing");
SendSmsRequest sendSmsRequest = new SendSmsRequest();
//appId
sendSmsRequest.setSmsSdkAppid(Contros.appId);
//发送短信的目标手机号,可填多个。
String[] phones={"+86"+number+""};
sendSmsRequest.setPhoneNumberSet(phones);
//模版id
sendSmsRequest.setTemplateID(Contros.templateId);
//生成随机6位数验证码
code = vcode();
System.out.println("验证码:"+code);
//模版参数,从前往后对应的是模版的{1}、{2}等
String [] templateParam={code};
sendSmsRequest.setTemplateParamSet(templateParam);
//签名内容
sendSmsRequest.setSign(Contros.Signed);
try {
SendSmsResponse sendSmsResponse= smsClient.SendSms(sendSmsRequest); //发送短信
System.out.println("字符窜为:"+SendSmsResponse.toJsonString(sendSmsResponse));
return SendSmsResponse.toJsonString(sendSmsResponse);
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
return "";
}
/**
* 生成6位随机数验证码
* @return
*/
public static String vcode(){
String vcode = "";
for (int i = 0; i < 6; i++) {
vcode = vcode + (int)(Math.random() * 9);
}
return vcode;
}
} [Java] 纯文本查看 复制代码 public class Contros {
//secretId
public static final String secretId = ""填写自己的";
//secretKey
public static final String secretKey = "填写自己的";
//appId
public static final String appId = ""填写自己的";
//模板ID
public static final String templateId = ""填写自己的";
//签名内容
public static final String Signed = "填写自己的";
} |