iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot整合腾讯云短信开箱即用的示例代码
  • 368
分享到

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

2024-04-02 19:04:59 368人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

引入腾讯云依赖 <!--腾讯云核心api--> <dependency> <groupId>com.tencentcloudapi&l

引入腾讯云依赖


 <!--腾讯云核心api-->
 <dependency>
  <groupId>com.tencentcloudapi</groupId>
  <artifactId>tencentcloud-sdk-java</artifactId>
  <version>3.1.111</version>
 </dependency>
 <dependency>
  <groupId>com.GitHub.qcloudsms</groupId>
  <artifactId>qcloudsms</artifactId>
  <version>1.0.6</version>
 </dependency>

其次配置属性类


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;



@Slf4j
public class TxSmsTemplate {

 private TxProperties txProperties;

 public TxSmsTemplate(TxProperties txProperties) {
 this.txProperties = txProperties;
 }


 
 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,
   txProperties.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;
 }
}

注册腾讯云短信到容器



@Configuration
@EnableConfigurationProperties({TxProperties.class})
public class CommonsAutoConfiguration {
 
 @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整合腾讯云短信内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springboot整合腾讯云短信开箱即用的示例代码

本文链接: https://www.lsjlt.com/news/122094.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作