• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot整合腾讯云短信开箱即用的示例代码

springboot 搞代码 4年前 (2022-01-05) 34次浏览 已收录 0个评论

这篇文章主要介绍了springboot整合腾讯云短信开箱即用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

引入腾讯云依赖

 <!--腾讯云核心API--> com.tencentcloudapitencentcloud-sdk-java3.1.111 com.github.qcloudsmsqcloudsms1.0.6

其次配置属性类

 import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * 腾讯云发送短信 配置信息类 */ @Data @ConfigurationProperties(prefix = "tanhua.txsms") // 读取application中的tanhua.sms的属性 public class TxProperties { // AppId 1400开头的 private int AppId; // 短信应用SDK AppKey private String AppKey; // 短信模板ID private int TemplateId; // 签名 private String signName; }

其次配置工具类

 package com.He.commons.templates; import com.He.commons.properties.TxProperties; import com.alibaba.fastjson.JSONException; import com.github.qcloudsms.SmsSingleSender; import com.github.qcloudsms.SmsSingleSenderResult; import com.github.qcloudsms.httpclient.HTTPException; import lombok.extern.slf4j.Slf4j; import java.io.IOException; /** * 腾讯云发送短信模板对象,封装了发送短信的api */ @Slf4j public class TxSmsTemplate { private TxProperties txProperties; public TxSmsTemplate(TxProperties txProperties) { this.txProperties = txProperties; } /** * 指定模板ID发送短信 * * @param number 用户手机号 * @return OK 成功 null 失败 */ public String sendMesModel(String number,String value) { try { String[] params = {value};//{参数} SmsSingleSender ssender = new SmsSingleSender(txProperties.getAppId(), txProperties.getAppKey()); SmsSingleSenderResult result = ssender.sendWithParam("86", number, txProper<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>ties.getTemplateId(), params, txProperties.getSignName(), "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信 System.out.print(result); return result.errMsg; //OK } catch (HTTPException e) { // HTTP响应码错误 log.info("短信发送失败,HTTP响应码错误!!!!!!!!!!!!"); // e.printStackTrace(); } catch (JSONException e) { // json解析错误 log.info("短信发送失败,json解析错误!!!!!!!!!!!!"); //e.printStackTrace(); } catch (IOException e) { // 网络IO错误 log.info("短信发送失败,网络IO错误!!!!!!!!!!!!"); // e.printStackTrace(); } return null; } }

注册腾讯云短信到容器中

 /** * 短信模块自动装配类 * 根据springboot自动装备原理 * 将smsTemplate对象注入到容器中 * 要配置META-INF/spring.factories */ @Configuration @EnableConfigurationProperties({TxProperties.class}) public class CommonsAutoConfiguration { /** * 注册txSmsTemplate到容器中 * @param txProperties * @return */ @Bean public TxSmsTemplate txSmsTemplate(TxProperties txProperties) { return new TxSmsTemplate(txProperties); } }

在resources中配置启动自动装配类

 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.He.commons.CommonsAutoConfiguration

最后测试一下

 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; @SpringBootTest @RunWith(SpringRunner.class) public class TXTest { @Autowired private TxSmsTemplate txSmsTemplate; @Test public void TestTxsms(){ String result = txSmsTemplate.sendMesModel("1511938****", "666666");//第一个参数为手机号,第二个发送短信的内容 System.out.println(result); // result等于OK 就表示发送成功 } }

返回OK则表示发送成功

到此这篇关于springboot整合腾讯云短信开箱即用的文章就介绍到这了,更多相关springboot整合腾讯云短信内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是springboot整合腾讯云短信开箱即用的示例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot整合腾讯云短信开箱即用的示例代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址