Tencent Cloud SMS Service User Guide

Introduce Tencent Cloud SMS service dependency

<!-- 引入腾讯云短信服务 依赖 -->
<dependency>
   <groupId>com.tencentcloudapi</groupId>
   <artifactId>tencentcloud-sdk-java</artifactId>
   <version>3.1.270</version>
 </dependency>

SMS sending tools

/**
 * @Title: 短信发送 工具类
 * @author hexiag
 */

@Component
public class SMSUtil {
    
    

    @Autowired
    TencentCloudProperties tencentCloudProperties;

    public void sendSMS(String phone, String code) throws Exception {
    
    
        try {
    
    
            /* 必要步骤:
             * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
             * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
             * 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
             * 以免泄露密钥对危及你的财产安全。
             * CAM密匙查询获取: https://console.cloud.tencent.com/cam/capi
             */
            Credential cred = new Credential(tencentCloudProperties.getSecretId(), tencentCloudProperties.getSecretKey());

            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();

            // httpProfile.setReqMethod("POST"); // 默认使用POST

            /* SDK会自动指定域名。通常是不需要特地指定域名的,但是如果你访问的是金融区的服务
             * 则必须手动指定域名,例如sms的上海金融区域名: sms.ap-shanghai-fsi.tencentcloudapi.com
             */
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            // 实例化一个client选项
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);

            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {
    
    "+86" + phone};//电话号码
            req.setPhoneNumberSet(phoneNumberSet1);
            req.setSmsSdkAppId("your_info"); // 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId
            req.setSignName("your_info"); // 签名
            req.setTemplateId("your_info"); // 模板id:必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看

            /* 模板参数(自定义占位变量): 若无模板参数,则设置为空 */
            String[] templateParamSet1 = {
    
    code};
            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());
        }
    }
    // Test 测试代码做校验
    //public static void main(String[] args) {
    
    
    //    try {
    
    
    //        new SMSUtils().sendSMS("13912345678", "985211");
    //    } catch (Exception e) {
    
    
    //        e.printStackTrace();
    //    }
    //}
}

Tencent cloud configuration

/**
 * @author: hexiang
 * @description: 腾讯云配置
 */

@Component
@Data
@PropertySource("classpath:tencentcloud.properties")
@ConfigurationProperties(prefix = "tencent.cloud")
public class TencentCloudProperties {
    
    

    private String secretId;
    private String secretKey;

}

tencentcloud.properties

tencent.cloud.secretID=your_info
tencent.cloud.secretKey=your_info

Activate Tencent Cloud SMS Service

  • Register Tencent Cloud
  • Personal real-name authentication
  • Enter the console and find the SMS in the cloud product

insert image description here

  • By default, it is not activated. After reading the service agreement, click to start accessing

insert image description here

  • Generally speaking, as long as the real name has been authenticated, the SMS service can be opened directly

insert image description here

  • After successful access, you can view the SMS control panel

insert image description here

  • Test SMS sending

insert image description here
insert image description here

  • quick start

insert image description here

insert image description here

insert image description here

  • Use your own WeChat to register an official account. I already have an official account here, so you can use the SMS service by using the official account.

insert image description here

  • Create SMS template

insert image description here

insert image description here

insert image description here

  • Next, wait patiently, basically within 1 hour, it is still relatively fast

insert image description here

  • Review SMS

insert image description here

  • For the first use, there will be 100 free text messages, which is more user-friendly than other cloud vendors

insert image description here

After the verification is successful, it can be sent

  • It should be noted that although the review is passed, it is necessary to wait for the template to take effect. There is a delay of several minutes, just wait for a while.

insert image description here

insert image description here

  • It worked in a few minutes

insert image description here

  • Test sending SMS manually

insert image description here

  • Only after the template is modified and used can the text message be sent

insert image description here

  • If the format is not low, then he will give you a corresponding prompt

insert image description here

Integrate code for SMS sending (preparation work)

  • create key

insert image description here

  • Create your own key

insert image description here

Guess you like

Origin blog.csdn.net/HXBest/article/details/129576629