1.腾讯云的短信集成可以直接参考官网的说明,这里的集成均来自官网,同时加注一些需要注意的细节;
腾讯云短信API文档:https://cloud.tencent.com/document/product/382/38778
SDK说明文档:https://cloud.tencent.com/document/product/382/43194
【腾讯云短信集成-JAVA】腾讯云在线生成sdk集成api代码:https://console.cloud.tencent.com/api/explorer?Product=sms&Version=2019-07-11&Action=SendSms&SignVersion=
2.集成讲解
maven集成,这里通过maven可以查看sdk包的版本,最新版是221(2021年3月17日),不过拿了几次包都拿不下来,就改用220版本了。
com.tencentcloudapi
tencentcloud-sdk-java
3.1.220
潦草的未经任何修饰的测试代码
package com.test.tencent;
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 net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* @Description:腾讯云短信
* @author : Just Test
* @date : 2021-3-17 11:32
*/
public class TencentSMSSupportGeneratorImpl {private static final Logger LOG = LoggerFactory.getLogger(TencentSMSSupportGeneratorImpl.class);
private static final String CHINA_CODE = "+86";
@Override
public boolean sendSMS(JSONObject resourceInfo, String params, String phone) throws Exception {
try{Credential cred = new Credential("secretId", "secretKey");
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("sms.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(cred, "", clientProfile);
SendSmsRequest req = new SendSmsRequest();
String[] phoneNumberSet1 = {"+86185********","+8613*********"};
req.setPhoneNumberSet(phoneNumberSet1);
req.setTemplateID("短信模板ID");
req.setSign("短信签名");
req.setTemplateParamSet(new String[]{"测试同志","2021年3月17日","成都"});
req.setSmsSdkAppid("140********");
SendSmsResponse resp = client.SendSms(req);
System.out.println(SendSmsResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return false;
}public static void main(String [] args) throws Exception {
TencentSMSSupportGeneratorImpl i_just_do_it = new TencentSMSSupportGeneratorImpl();
i_just_do_it.sendSMS(param,"","");
}
}
注意点:
1.这里只是国内短信版本,国际短信没有研究资源;
2.国内短信发送,手机号不能直接用,要加“+86”,不然会报错,api文档会有报错解释;
3.模板参数区别于其他平台的json格式,这里的模板参数是数组。申请模板的时候写{1},{2},{3},举个栗子:
{1},您好: 您的账号于{2}在{3}登录,系统检测到您存在账号异地登录的情况,请注意保护账号安全。
这个短信模板就写这个样子:new String[]{"测试同志","2021年3月17日","成都"}
着重注释:签名很难申请,要好好珍惜来之不易的签名。