Java docking Tencent SMS service

1. Preparation before development

1. Create a signature

Click this to create a template, or click Domestic SMS "Signature Management" on the left.
Here you need a registered website. If you don't have a registered website, you can buy one on Tencent Cloud and then register it on Tencent Cloud.
Insert image description here
Insert image description here

2. Create a template

Click "Domestic SMS" on the left side "Text Template Management" to create one
Insert image description here

3. Click Application Management > Application List on the left to create an application. If there is a default application, you can also use the default application. The SDK AppID and App Key in the application will be used later.

4. Create an access key, which is also needed in the subsequent springboot configuration.

Insert image description here
When you create a sub-user, you can select permissions for text messages to the sub-user. You can also not assign permissions. If not assigned, the default should be all permissions. Of course, it is up to you whether to assign permissions or not. Obtain the key after creating it
Insert image description here
.
Insert image description here

2. Code writing

1.pom file

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

2. Configuration file

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

3.Code

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());
        }

    }
}

Guess you like

Origin blog.csdn.net/mcband/article/details/129622070