JAVA SpringBoot整合腾讯短信

如果帮到你,辛苦点个赞吧~

第一步

引入依赖。

<!-- 腾讯云短信 -->
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>3.1.59</version>
</dependency>

第二步

配置application.yml文件

#腾讯云
tencent:
  #查看密钥地址:https://console.cloud.tencent.com/cam/capi 
  secretId: 密钥ID
  secretKey: 密钥KEY
  sms:
    regionId: ap-beijing
    SDKAppId: 短信的应用ID
    AppKey: 应用的APPKEY
   satellitemessage: 短信模板编号
   signName: 签名

第三步

增加工具类,直接copy即可。
Result是我自己定义的,你可以自己写一个哈。

import com.aliyuncs.exceptions.ClientException;
import com.penghu.utils.Result;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author :Mall
 * @date :Created in 2022-06-17
 * @description :腾讯云短信服务
 */



public class TencentSmsSendUtil {
    ("${tencent.secretId}")
    private String secretId;

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

    ("${tencent.sms.SDKAppId}")
    private String SDKAppId;

    ("${tencent.sms.AppKey}")
    private String AppKey;

    ("${tencent.sms.regionId}")
    private String regionId;

    ("${tencent.sms.signName}")
    private String signName;
    //卫星消息
    ("${tencent.sms.satellitemessage}")
    private String satelliteMessage;


    /**
     * 发送消息
     *
     * @param number   电话号码
     * @param mobileId 终端编号
     * @return
     * @throws ClientException
     */
    public Result sendMessage(String number, String mobileId, String message, String map, String point) {
        Credential cred = new Credential(this.secretId, this.secretKey);
        SmsClient client = new SmsClient(cred, this.regionId, new ClientProfile());
        SendSmsRequest req = new SendSmsRequest();
        String[] numberList = number.split("[,]");
        for (int i = 0; i < numberList.length; i++) {
            if (!numberList[i].startsWith("+86")) {
                numberList[i] = "+86" + numberList[i];
            }
        }

        req.setPhoneNumberSet(numberList);
        req.setSign(this.signName);
        req.setTemplateID(this.satelliteMessage);
        req.setSmsSdkAppid(this.SDKAppId);
        //这里替换变量从{1} 到 {4}
        req.setTemplateParamSet(new String[]{mobileId, message, map, point});
        try {
            SendSmsResponse res = client.SendSms(req);
            for (int i = 0; i < res.getSendStatusSet().length; i++) {
                SendStatus sendStatus = res.getSendStatusSet()[i];
                if (!sendStatus.getCode().equals("Ok")) {
                    log.info(sendStatus.getPhoneNumber() + "发送失败,Code:" + sendStatus.getCode());
                }
            }
            return new Result().error("发送完成");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("发送失败");
        }
    }


}

你可能感兴趣的:(java,spring,boot,腾讯云)