java对接腾讯短信服务

一.开发前准备

1. 创建一个签名

点击这个创建模板,或者点击左侧的 国内短信》签名管理
这里需要一个已经备案好的网站,没有备案网站的可以在腾讯云买一个,然后在腾讯云备案
在这里插入图片描述
在这里插入图片描述

2. 创建模板

点击左侧 国内短信》正文模板管理,去创建一个
在这里插入图片描述

3. 点击左侧的 应用管理》应用列表,创建一个应用,如果有默认应用,使用默认应用也可以,应用中的SDK AppID和App Key都是后面需要用到的

4. 创建一个访问密匙,也是后面springboot配置中需要的

在这里插入图片描述
创建一个子用户,可以给子用户选择短信的权限,你也可以不分配权限,不分配默认应该就是全部权限,当然分不分随你自己
在这里插入图片描述
创建好过后获取密钥
在这里插入图片描述

二.代码编写

1.pom文件

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

2.配置文件

tencent:
  sms:
    # 短信应用ID:短信SdkAppId在 【短信控制台】添加应用后生成实际的SdkAppId
    appId: 
    # 短信签名内容:
    signName: 
    # 腾讯云账户 secrtId,secretKey
    secretId: 
    secretKey: 

3.代码

package com.sinosoft.springbootplus.sms.service.impl;

import com.tencentcloudapi.common.Credential;

import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 腾讯短信 实现类
 * </pre>
 *
 * @author mc
 * @since 2023/3/16
 */
@Service
@Slf4j
public class TcSmsServiceImpl{
    
    

    @Value("${tencent.sms.appId}")
    private String appId;

    @Value("${tencent.sms.secretId}")
    private String secretId;

    @Value("${tencent.sms.secretKey}")
    private String secretKey;

    @Value("${tencent.sms.signName}")
    private String signName;

    public String getSignName() {
    
    
        return signName;
    }

    public String getSecretId() {
    
    
        return secretId;
    }

    public String getSecretKey() {
    
    
        return secretKey;
    }

    public String getAppId() {
    
    
        return appId;
    }
    
    /**
     * 批量短信发送
     *
     * @param phoneNumbers  电话号码
     * @param signName     每条签名
     * @param templateCode  模板编码
     * @param templateParam 每条参数
     */
    public void sendSms(String[] phoneNumbers,String signName, String templateCode, String[] templateParam) {
    
    
        try{
    
    
            if (StringUtils.isBlank(signName)) {
    
    
                signName = getSignName();
            }
            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential(getSecretId(), getSecretKey());
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(phoneNumbers);
            req.setSmsSdkAppId(getAppId());
            req.setSignName(signName);
            req.setTemplateId(templateCode);
            req.setTemplateParamSet(templateParam);
            // 返回的resp是一个SendSmsResponse的实例,与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
    
    
            System.out.println(e.toString());
        }

    }
}

猜你喜欢

转载自blog.csdn.net/mcband/article/details/129622070