腾讯云短信服务的简单使用

1、腾讯云短信服务

  1. 开通短信服务
  2. 设置短信签名(审核时可以利用微信公众号)
    在这里插入图片描述
  3. 设置短信模板
    在这里插入图片描述
    4、创建应用,获取SDKAppID
    在这里插入图片描述
    5、获取secreteid和secretekey,身份唯一标识
    在这里插入图片描述

2、腾讯云自动生成发送验证码的java代码

先进入短信服务,点击云产品–云api
在这里插入图片描述
选择发送短信(SMS)
在这里插入图片描述
填入发送短信的相关参数
在这里插入图片描述
测试发送验证码

3、在maven工程中接入短信服务

pom.xml导入依赖

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

编写测试类

 public static void main(String [] args) {
        try{
            // 注意点:填写自己的secreteid和secretekey,密钥在腾讯云访问密钥获取
            Credential cred = new Credential("SecretId", "SecretKey");
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            //注意点:+86必须要,为国内号码,不然会识别不了国内号码
            String[] phoneNumberSet1 = {"+8618773227702"};
            req.setPhoneNumberSet(phoneNumberSet1);
            
            //设置自己的SdkAppID
            req.setSmsSdkAppid("1400515730");
            //设置自己短信签名名称,不是签名id
            req.setSign("Devin的小管家");
            //设置自己短信模板id
            req.setTemplateID("959096");

			//设置验证码
            String[] templateParamSet1 = {"666666"};
            req.setTemplateParamSet(templateParamSet1);

            // 返回的resp是一个SendSmsResponse的实例,与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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(); } } } ``` 需要注意的是,腾讯云短信服务需要进行实名认证才能使用。另外,短信发送需要消耗一定的费用,具体费用可以在腾讯云控制台中查询。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值