Java实现腾讯云短信发送

引入依赖

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

核心代码如下:

public class SendSmsUtil {

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

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

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

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

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


    public String sendSmsUtil(String phone) {
        String code = RandomUtil.getRandomToNum(6);

        try {
            //实例化认证对象
            Credential credential = new Credential(secretId, secretKey);
            //创建http连接
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("POST");
            httpProfile.setConnTimeout(60);
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            //实例化客户端对象
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(credential, "ap-guangzhou", clientProfile);

            //实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
            SendSmsRequest req = new SendSmsRequest();

        /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
            // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看

            req.setSmsSdkAppId(appId);

            /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
            // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
            req.setSignName(signName);

            /* 模板 ID: 必须填写已审核通过的模板 ID */
            // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
            req.setTemplateId(templateId);

            /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
            String[] templateParamSet = {code};
            req.setTemplateParamSet(templateParamSet);

            /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
             * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 */
            String[] phoneNumberSet = {"+86"+phone};
            req.setPhoneNumberSet(phoneNumberSet);

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

            /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
            String extendCode = "";
            req.setExtendCode(extendCode);

            /* 国际/港澳台短信 SenderId(无需要可忽略): 国内短信填空,默认未开通,如需开通请联系 [腾讯云短信小助手] */
            String senderid = "";
            req.setSenderId(senderid);


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

            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(res));
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return code;
    }
}

一些参数自行去腾讯云查看即可

刚购买短信服务的时候,需要去短信控制台里面申请签名和短信模板,就可以得到signName和 templateId

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用腾讯云发送短信服务,需要进行以下步骤: 1. 注册腾讯云账号,并开通短信服务。 2. 在腾讯云控制台,创建应用和签名模板。应用表示您的业务场景,签名模板表示发送短信的签名和模板内容。 3. 在Java项目引入腾讯云SDK,可以通过Maven依赖来引入。 4. 创建短信发送API,并调用腾讯云SDK提供的发送短信接口。 下面是一个简单的示例代码,可以参考: ``` import com.tencentcloudapi.common.Credential; 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; public class TencentSmsSender { public static void main(String[] args) { String secretId = "your_secret_id"; String secretKey = "your_secret_key"; String appid = "your_appid"; String sign = "your_sign"; // 签名名称 String templateId = "your_template_id"; // 模板 ID try { Credential cred = new Credential(secretId, secretKey); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("sms.tencentcloudapi.com"); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); SmsClient client = new SmsClient(cred, "", clientProfile); SendSmsRequest req = new SendSmsRequest(); req.setSmsSdkAppid(appid); req.setSign(sign); req.setTemplateID(templateId); req.setPhoneNumberSet(new String[]{"+86138xxxx0000"}); // 接收短信的手机号码 req.setTemplateParamSet(new String[]{"code=123456"}); // 短信模板的参数 SendSmsResponse res = client.SendSms(req); System.out.println(SendSmsResponse.toJsonString(res)); } catch (TencentCloudSDKException e) { e.printStackTrace(); } } } ``` 需要注意的是,腾讯云短信服务需要进行实名认证才能使用。另外,短信发送需要消耗一定的费用,具体费用可以在腾讯云控制台查询。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值