引入依赖

<!-- 腾讯云依赖 -->
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>3.1.270</version>
</dependency>

配置文件

# 腾讯云短信配置
sms:
  tencentcloud:
    enabled: true
    region-id: xxx
    app-id: xxx
    secret-id: xxx
    secret-key: xxx
    sign-name: xxx
    template-id: xxx

属性类

package com.qiangesoft.sms.tencentcloud.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 腾讯云短信属性
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@Data
@ConfigurationProperties(prefix = "sms.tencentcloud")
public class TencentcloudSmsProperties {

    /**
     * 是否启用
     */
    private boolean enabled = true;

    /**
     * 地域id
     */
    private String regionId;
    
    /**
     * appId
     */
    private String appId;

    /**
     * secretId
     */
    private String secretId;

    /**
     * secretKey
     */
    private String secretKey;

    /**
     * 短信签名
     */
    private String signName;

    /**
     * 模板Id
     */
    private String templateId;

}

配置类

package com.qiangesoft.sms.tencentcloud.config;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 腾讯云短信配置
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@Configuration
@EnableConfigurationProperties(TencentcloudSmsProperties.class)
@ConditionalOnProperty(prefix = "sms.tencentcloud", name = "enabled", havingValue = "true")
public class TencentcloudSmsConfiguration {

    private static final String ENDPOINT = "sms.tencentcloudapi.com";

    @Autowired
    private TencentcloudSmsProperties tencentcloudSmsProperties;

    @Bean
    @ConditionalOnMissingBean
    public SmsClient smsClient() {
        // 实例化一个认证对象
        Credential cred = new Credential(tencentcloudSmsProperties.getSecretId(), tencentcloudSmsProperties.getSecretKey());

        // 实例化一个http选项,可选
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("POST");
        httpProfile.setConnTimeout(60);
        httpProfile.setEndpoint(ENDPOINT);

        // 实例化一个客户端配置对象
        ClientProfile clientProfile = new ClientProfile();
        // SDK默认用TC3-HMAC-SHA256进行签名,非必要请不要修改这个字段
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        return new SmsClient(cred, tencentcloudSmsProperties.getRegionId(), clientProfile);
    }

}

短信发送实现

package com.qiangesoft.sms.tencentcloud.handler;

import com.qiangesoft.sms.tencentcloud.config.TencentcloudSmsProperties;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 腾讯云短信处理器
 *
 * @author qiangesoft
 * @date 2023-05-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TencentcloudSendHandler {

    private final SmsClient smsClient;

    private final TencentcloudSmsProperties tencentcloudSmsProperties;

    public boolean send(String[] phones, String[] params) {
        return this.send(phones, tencentcloudSmsProperties.getTemplateId(), params);
    }

    public boolean send(String[] phones, String templateId, String[] params) {
        // 填充请求参数
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppId(tencentcloudSmsProperties.getAppId());
        req.setSignName(tencentcloudSmsProperties.getSignName());
        req.setTemplateId(templateId);
        req.setTemplateParamSet(params);
        req.setPhoneNumberSet(phones);

        try {
            // 方法发起请求
            SendSmsResponse res = smsClient.SendSms(req);
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

}

测试

package com.qiangesoft.sms.tencentcloud.controller;

import com.qiangesoft.sms.tencentcloud.handler.TencentcloudSendHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信测试
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@RequestMapping("/tsms")
@RestController
public class TSmsController {

    @Autowired
    private TencentcloudSendHandler tencentcloudSendHandler;

    @GetMapping("/send")
    public Boolean send() {
        String[] params = {"张三", "23"};
        return tencentcloudSendHandler.send(new String[]{"15xxxxxxxxx"}, params);
    }

}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐