Spring Boot 整合 腾讯云短信(模板模式)
参考项目: RuoYi-Vue-Plus
添加依赖
<dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><version>3.1.500</version></dependency>
配置相关
配置项
#--- # sms 短信
sms:enabled: trueendpoint: "sms.tencentcloudapi.com"accessKeyId: XXXXXXaccessKeySecret: XXXXXXsignName: sdkAppId:
相关代码
配置文件 SmsProperties
映射类
/*** SMS短信 配置属性** @author Lion Li* @version 4.2.0*/
@Data
@Component
@ConfigurationProperties(prefix = "sms")
public class SmsProperties {private Boolean enabled;/*** 配置节点* 腾讯云 sms.tencentcloudapi.com*/private String endpoint;/*** key*/private String accessKeyId;/*** 密匙*/private String accessKeySecret;/** 短信签名*/private String signName;/*** 短信应用ID (腾讯专属)*/private String sdkAppId;}
配置类 SmsConfig
@Configuration
@ConditionalOnProperty(value = "sms.enabled", havingValue = "true")
public class SmsConfig {@Beanpublic SmsTemplate tencentSmsTemplate(SmsProperties smsProperties) {return new TencentSmsTemplate(smsProperties);}}
模板接口类 SmsTemplate
/*** 短信模板** @author Lion Li* @version 4.2.0*/
public interface SmsTemplate {/*** 发送短信** @param phones 电话号(多个逗号分割)* @param templateId 模板id* @param param 模板对应参数* 腾讯 需使用 模板变量顺序对应内容 例如: 1=1234, 1为模板内第一个参数*/SmsResult send(String phones, String templateId, Map<String, String> param);}
模块实现类 TencentSmsTemplate
/*** Tencent 短信模板** @author Lion Li* @version 4.2.0*/
public class TencentSmsTemplate implements SmsTemplate {private SmsProperties properties;private SmsClient client;@SneakyThrows(Exception.class)public TencentSmsTemplate(SmsProperties smsProperties) {this.properties = smsProperties;Credential credential = new Credential(smsProperties.getAccessKeyId(), smsProperties.getAccessKeySecret());HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint(smsProperties.getEndpoint());ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);this.client = new SmsClient(credential, "", clientProfile);}public SmsResult send(String phones, String templateId, Map<String, String> param) {if (StringUtils.isBlank(phones)) {throw new SmsException("手机号不能为空");}if (StringUtils.isBlank(templateId)) {throw new SmsException("模板ID不能为空");}SendSmsRequest req = new SendSmsRequest();Set<String> set = Arrays.stream(phones.split(",")).map(p -> "+86" + p).collect(Collectors.toSet());req.setPhoneNumberSet(ArrayUtil.toArray(set, String.class));if (CollUtil.isNotEmpty(param)) {req.setTemplateParamSet(ArrayUtil.toArray(param.values(), String.class));}req.setTemplateID(templateId);req.setSign(properties.getSignName());req.setSmsSdkAppid(properties.getSdkAppId());try {SendSmsResponse resp = client.SendSms(req);SmsResult.SmsResultBuilder builder = SmsResult.builder().isSuccess(true).message("send success").response(resp);for (SendStatus sendStatus : resp.getSendStatusSet()) {if (!"Ok".equals(sendStatus.getCode())) {builder.isSuccess(false).message(sendStatus.getMessage());break;}}return builder.build();} catch (Exception e) {throw new SmsException(e.getMessage());}}}
结果类 SmsResult
/*** 上传返回体** @author Lion Li*/
@Data
@Builder
public class SmsResult {/*** 是否成功*/private boolean isSuccess;/*** 响应消息*/private String message;/*** 实际响应体*/private Object response;
}
Spring Boot 整合 腾讯云短信(模板模式)相关推荐
- Spring Boot 整合 阿里云短信(模板模式)
参考项目: RuoYi-Vue-Plus 添加依赖 <dependency><groupId>com.aliyun</groupId><artifactId& ...
- Java整合腾讯云短信发送
1. 引入相关maven依赖 <dependency><groupId>com.github.qcloudsms</groupId><artifactId&g ...
- 使用腾讯云短信服务实现Spring Boot短信发送
发送短信 01 注册微信公众号 02 注册腾讯云账号 03 创建签名和模板 第一步 创建签名 第二部 创建模板 04 发送短信 01 注册微信公众号 在百度搜索微信公众平台,并在进入官网后注册.在账号 ...
- SpringBoot集成腾讯云短信实现注册/登录功能
前言 笔记参考 Duktig丶 JavaSDK地址:短信 Java SDK - SDK 文档 - 文档中心 - 腾讯云 1.导入依赖 <dependency><groupId> ...
- 使用Python开通腾讯云短信(sms)【亲测有效·超详细】
使用Python开通腾讯云短信(sms) 1.开通腾讯云短信服务 2.创建应用 3.创建签名 4.Python 操作腾讯云短信接口 模块安装 详细代码[可直接作为接口] 1.开通腾讯云短信服务 腾讯云 ...
- 如何通过腾讯云短信实现发送验证码并校验验证码以实现登录功能
如何通过腾讯云短信实现发送验证码并校验验证码以实现登录功能 验证码相关的10种技术 图像处理技术:生成.识别.验证验证码的图像. 机器学习技术:让计算机自动学习并识别验证码. 文字识别技术:将图像中的 ...
- 快速集成腾讯云短信实现短信发送(Spring Boot)
前言 腾讯云活动白嫖 1.地址 地址:https://cloud.tencent.com/act/free?from=14600 2.创建签名(根据实际情况选择) 我的是网站 3.创建模板 选择模板内 ...
- Spring Boot腾讯云短信申请
1.在pom.xml文件中添加依赖 <dependency><groupId>com.tencentcloudapi</groupId><artifactId ...
- Spring Boot中使用腾讯云短信服务
第一步:在腾讯云官方网站开通短信服务 第二部:开通后,在短信控制面板中找到国内短信 第三步:点击签名管理并创建签名 第五步:点击正文模板管理并创建正文模板 第六步:在pom文件中添加腾讯云短信依赖 & ...
最新文章
- 如何在Node.js中退出
- HTML5 MediaStream的运用
- python一个月能学成嘛-python自我学习1--class 面向对象变成
- 【深度学习】何恺明经典之作—2009 CVPR Best Paper | Dark Channel Prior
- xdebug怎样在php中配置,教你在PHPStorm中配置Xdebug
- Log4j.properties的简单配置
- 面试:InnoDB 并发控制
- 大数据学习笔记17:MR案例——分区统计总利润与平均利润
- 小红书公司注册老红书商标上热搜 网友:过两年变老了改名吗?
- java 无法显示打印页面_使用打印机打印网页时出现的脚本错误的办法
- CodeForces 68 A.Irrational problem(水~)
- 最全面计算机英语单词列表(四)
- python字典包含字典_Python字典教学
- 思科EA3500官方固件刷opwrt教程
- PDF Expert快捷键
- 微信小程序实现讯飞语音合成
- java进度条_Java web实时进度条整个系统共用(如java上传、下载进度条、导入、导出excel进度条等)...
- 超级内存NVDIMM
- 7-3 求100以内的素数
- springboot项目导入idea中环境配置相关问题解决