赞
踩
1 打开腾讯云短信服务,领取免费短信
2 打开腾讯公众号申请平台,申请公众号,申请类型选择个人类型。
记录下公平号名称,
3 登录腾讯云短信发送平台,按照步骤申请国内短信发送
4 申请短信签名和短信模板,等待审核通过,获取审核通过的短信模板ID和短信签名
5 获取腾讯密钥: secretId和secretKey
6 获取个人短信SDK
7 下载短信发送依赖
- <!--腾讯云短信-->
- <dependency>
- <groupId>com.tencentcloudapi</groupId>
- <artifactId>tencentcloud-sdk-java</artifactId>
- <version>3.1.424</version>
- </dependency>
1 创建短信发送配置类,代码如下:
package com.hqyj.config; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.sms.v20210111.SmsClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TencentSmsConfig { @Bean public SmsClient getSmsCilent(){ //创建腾讯云短信发送凭证对象,填入secretId和secretKey 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, "ap-guangzhou", clientProfile); return client; } }
2 创建短信发送工具类
- package com.hqyj.utile;
-
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.tencentcloudapi.common.exception.TencentCloudSDKException;
- import com.tencentcloudapi.sms.v20210111.SmsClient;
- import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
- import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
-
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
-
- @Component
- public class SmsSend {
-
- @Autowired
- SmsClient smsClient;
-
- public HashMap<String,Object> send(String tel) throws TencentCloudSDKException {
- HashMap<String,Object> map = new HashMap<String,Object>();
- //创建短信发送请求对象
- SendSmsRequest req = new SendSmsRequest();
- //接收短信手机号
- String[] phone = {tel};
- req.setPhoneNumberSet(phone);
- //个人短信模板模板Id
- req.setTemplateId("短信模板模板Id");
- //个人短信发送SDK
- req.setSmsSdkAppId("短信发送SDK");
- //个人短信签名
- req.setSignName("短信签名");
- //短信验证码
- String[] p = {"2345"};
- req.setTemplateParamSet(p);
- //发送对象,并获取获取响应对象
- SendSmsResponse resp = smsClient.SendSms(req);
- //将响应结果转换成字符串格式
- String info = SendSmsResponse.toJsonString(resp);
- // 打印结果信息
- System.out.println(info);
-
- try {
- //将字符串格式转换成Map 接口对象,获取短信发送成功的提示
- ObjectMapper mapper = new ObjectMapper();
- Map<String,Object> m = mapper.readValue(info, Map.class);
- List<Map<String,Object>> list =( List<Map<String,Object>>) m.get("SendStatusSet");
- System.out.println(list.get(0).get("Code"));
- if(list.get(0).get("Code").equals("Ok")){
- System.out.println("发送成功");
- }
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
-
- return map;
- }
- }
3 创建测试类,测试
- package com.hqyj.utile;
-
- import com.hqyj.MyApp;
- import com.tencentcloudapi.common.exception.TencentCloudSDKException;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- import static org.junit.Assert.*;
-
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = MyApp.class)
- public class SmsSendTest {
-
- @Autowired
- SmsSend smsSend;
- @Test
- public void send() {
- try {
- smsSend.send("电话");
- } catch (TencentCloudSDKException e) {
- e.printStackTrace();
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。