腾讯云短信集成-JAVA

1.腾讯云的短信集成可以直接参考官网的说明,这里的集成均来自官网,同时加注一些需要注意的细节;

腾讯云短信API文档:https://cloud.tencent.com/document/product/382/38778

SDK说明文档:https://cloud.tencent.com/document/product/382/43194

腾讯云在线生成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版本了。

        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.220</version>
        </dependency>

潦草的未经任何修饰的   测试代码 

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日","成都"}

 

着重注释:签名很难申请,要好好珍惜来之不易的签名。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值