JAVA整合腾讯云实现短信发送功能

腾讯云的官网: https://cloud.tencent.com/
选择云产品-短信

申请签名

选择国内短信-签名管理,点击创建签名申请项目所需的签名,签名内容的值就是下面代码中的sign的值
在这里插入图片描述

创建模版

选择正文模版管理,点击创建正文模版,填写自己业务所需的短信内容,参数写为{1},{2}等,创建成功后的id即为代码中需要填写的templateId参数值
在这里插入图片描述

SDKAppID,AppKey

选择应用管理-应用列表,点击创建应用,创建属于当前项目的应用模块,创建成功后点击该模块,拿到工具类中的SDKAppID和AppKey的参数值
在这里插入图片描述
在这里插入图片描述

SecreId、SecreKey

点击右上角个人中心中的访问管理
在这里插入图片描述
选择访问密钥-API密钥管理,从密钥中拿到的工具类中的SecretId和SecretKey的参数值
在这里插入图片描述

引入依赖

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

工具类

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 lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import com.tencentcloudapi.common.Credential;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Setter
@Getter
@Slf4j
public class SMSNoticeUtil {

    private final static String SECRET_ID = "";
    private final static String SECRET_KEY = "";
    private final static String END_POINT = "sms.tencentcloudapi.com";
    private final static String SDK_APP_ID = "";
    //短信签名
    private final static String SIGN = "";

    public static void process(String templateId, List<String> phoneNumberList, String[] params) {
        // 判断需要发送人员手机号码数组是否为空
        if (CollectionUtils.isEmpty(phoneNumberList)) {
            return;
        }
        // 加区号
        phoneNumberList = phoneNumberList.stream().map(val -> "86" + val).collect(Collectors.toList());
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("POST");
            httpProfile.setConnTimeout(60);
            httpProfile.setEndpoint(END_POINT);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "", clientProfile);
            SendSmsRequest req = new SendSmsRequest();

            req.setSmsSdkAppid(SDK_APP_ID);
            req.setSign(SIGN);

            // 国际/港澳台短信 senderId: 国内短信填空,默认未开通,如需开通请联系 [sms helper]
            String senderId = "";
            req.setSenderId(senderId);

            // 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回
            String session = "";
            req.setSessionContext(session);

            // 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper]
            String extendCode = "";
            req.setExtendCode(extendCode);

            // 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看
            req.setTemplateID(templateId);

            /* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
             * 例如+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
            String[] s = new String[phoneNumberList.size()];
            req.setPhoneNumberSet(phoneNumberList.toArray(s));

            log.info("即将给以下手机号发送短信::{}", phoneNumberList);

            // 模板参数: 若无模板参数,则设置为空
            req.setTemplateParamSet(params);

            /* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
             * 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
            SendSmsResponse res = client.SendSms(req);

            // 输出 JSON 格式的字符串回包
            log.info("短信发送完毕,返回信息::{}", SendSmsResponse.toJsonString(res));

        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            log.error("短信发送失败" + e.getMessage());
        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值