Java实现腾讯云发送短信

Java实现腾讯云发送短信

1.导入jar包

<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>3.1.111</version>
</dependency>
<dependency>
    <groupId>com.github.qcloudsms</groupId>
    <artifactId>qcloudsms</artifactId>
    <version>1.0.6</version>
</dependency>

2.工具类代码

package com.es.biz.common.utils;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
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;
import org.springframework.stereotype.Component;

import javax.xml.ws.http.HTTPException;
import java.io.IOException;


@Component
public class SmsUtils {


    private final static String appurl = "sms.tencentcloudapi.com";
    private final static String SDKAppID = "xxxxxxx";
    private final static String secretId = "xxxxxxx";
    private final static String secretKey = "xxxxxxx";
    private final static String SDKAppKey = "xxxxxxx";

    /**
     * 发送短信
     * @param templateId 短信模板id
     * @param nationCode 地区码
     * @param phoneNumbers
     * @return
     */
    public static SendSmsResponse sendSms(String templateId, String nationCode, String phoneNumbers) {
        SendSmsResponse res = new SendSmsResponse();
        try {
            /* 必要步骤:
             * 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey
             * 本示例采用从环境变量读取的方式,需要预先在环境变量中设置这两个值
             * 您也可以直接在代码中写入密钥对,但需谨防泄露,不要将代码复制、上传或者分享给他人
             * CAM 密钥查询:https://console.cloud.tencent.com/cam/capi*/
            Credential cred = new Credential(secretId,secretKey);

            // 实例化一个 http 选项,可选,无特殊需求时可以跳过
            HttpProfile httpProfile = new HttpProfile();
            /* SDK 默认使用 POST 方法。
             * 如需使用 GET 方法,可以在此处设置,但 GET 方法无法处理较大的请求 */
            httpProfile.setReqMethod("POST");
            /* SDK 有默认的超时时间,非必要请不要进行调整
             * 如有需要请在代码中查阅以获取最新的默认值 */
            httpProfile.setConnTimeout(60);
            /* SDK 会自动指定域名,通常无需指定域名,但访问金融区的服务时必须手动指定域名
             * 例如 SMS 的上海金融区域名为 sms.ap-shanghai-fsi.tencentcloudapi.com */
            httpProfile.setEndpoint(appurl);

            /* 非必要步骤:
             * 实例化一个客户端配置对象,可以指定超时时间等配置 */
            ClientProfile clientProfile = new ClientProfile();
            /* SDK 默认用 TC3-HMAC-SHA256 进行签名
             * 非必要请不要修改该字段 */
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            /* 实例化 SMS 的 client 对象
             * 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量 */
            SmsClient client = new SmsClient(cred, "",clientProfile);
            /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
             * 您可以直接查询 SDK 源码确定接口有哪些属性可以设置
             * 属性可能是基本类型,也可能引用了另一个数据结构
             * 推荐使用 IDE 进行开发,可以方便地跳转查阅各个接口和数据结构的文档说明 */
            SendSmsRequest req = new SendSmsRequest();

            /* 填充请求参数,这里 request 对象的成员变量即对应接口的入参
             * 您可以通过官网接口文档或跳转到 request 对象的定义处查看请求参数的定义
             * 基本类型的设置:
             * 帮助链接:
             * 短信控制台:https://console.cloud.tencent.com/smsv2
             * sms helper:https://cloud.tencent.com/document/product/382/3773 */

            /* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666 */
            req.setSmsSdkAppid(SDKAppID);

            /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 */
            String sign = "xxxx"; //短信签名
            req.setSign(sign);

            /* 模板 ID: 必须填写已审核通过的模板 ID,可登录 [短信控制台] 查看模板 ID */
            req.setTemplateID(templateId);
            String phone = nationCode + phoneNumbers;

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

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

            // 输出 JSON 格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(res));

            // 可以取出单个值,您可以通过官网接口文档或跳转到 response 对象的定义处查看返回字段的定义
            System.out.println(res.getRequestId());

        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 验证码
     * @param phone
     * @param nationCode 国家/地区编号
     * @param code 验证码
     * @param templateId 短信模板id
     * @return
     */
    public static SmsSingleSenderResult sendCode(String phone, String nationCode, String code, Integer templateId){
        int appid = xxxxxxx;
        String[] phoneNumbers = {phone}; //手机号可以添很多。
        String smsSign = "xxxx"; //短信签名
        nationCode = nationCode.substring(1);
        SmsSingleSenderResult result = new SmsSingleSenderResult();
        try {
            String[] params = {code};  //第一个参数传递{1}位置想要的内容,第二个传递{2}的内容,以此类推。
            SmsSingleSender sender = new SmsSingleSender(appid, SDKAppKey);
            result = sender.sendWithParam(nationCode, phoneNumbers[0],
                    templateId, params, smsSign, "", "");
            System.out.println(result);
        } catch (HTTPException | JSONException | IOException | com.github.qcloudsms.httpclient.HTTPException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
//        SendSmsResponse result = sendSms("742718", "+86","18559659000");

        SmsSingleSenderResult result2 = sendCode("18559659000", "+86", "123456", 742756);
        JSONObject resultObject = JSONObject.parseObject(String.valueOf(result2));
        String msg = resultObject.getString("errmsg");
        if("OK".equals(msg)){
            System.out.println("发送成功");
        }
    }
}

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值