springboot整合騰訊云短信開(kāi)箱即用的示例代碼
引入騰訊云依賴(lài)
<!--騰訊云核心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>
其次配置屬性類(lèi)
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 騰訊云發(fā)送短信 配置信息類(lèi)
*/
@Data
@ConfigurationProperties(prefix = "tanhua.txsms") // 讀取application中的tanhua.sms的屬性
public class TxProperties {
// AppId 1400開(kāi)頭的
private int AppId;
// 短信應(yīng)用SDK AppKey
private String AppKey;
// 短信模板ID
private int TemplateId;
// 簽名
private String signName;
}
其次配置工具類(lèi)
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;
/**
* 騰訊云發(fā)送短信模板對(duì)象,封裝了發(fā)送短信的api
*/
@Slf4j
public class TxSmsTemplate {
private TxProperties txProperties;
public TxSmsTemplate(TxProperties txProperties) {
this.txProperties = txProperties;
}
/**
* 指定模板ID發(fā)送短信
*
* @param number 用戶(hù)手機(jī)號(hào)
* @return OK 成功 null 失敗
*/
public String sendMesModel(String number,String value) {
try {
String[] params = {value};//{參數(shù)}
SmsSingleSender ssender = new SmsSingleSender(txProperties.getAppId(), txProperties.getAppKey());
SmsSingleSenderResult result = ssender.sendWithParam("86", number,
txProperties.getTemplateId(), params, txProperties.getSignName(), "", ""); // 簽名參數(shù)未提供或者為空時(shí),會(huì)使用默認(rèn)簽名發(fā)送短信
System.out.print(result);
return result.errMsg; //OK
} catch (HTTPException e) {
// HTTP響應(yīng)碼錯(cuò)誤
log.info("短信發(fā)送失敗,HTTP響應(yīng)碼錯(cuò)誤!!!!!!!!!!!!");
// e.printStackTrace();
} catch (JSONException e) {
// json解析錯(cuò)誤
log.info("短信發(fā)送失敗,json解析錯(cuò)誤!!!!!!!!!!!!");
//e.printStackTrace();
} catch (IOException e) {
// 網(wǎng)絡(luò)IO錯(cuò)誤
log.info("短信發(fā)送失敗,網(wǎng)絡(luò)IO錯(cuò)誤!!!!!!!!!!!!");
// e.printStackTrace();
}
return null;
}
}
注冊(cè)騰訊云短信到容器中
/**
* 短信模塊自動(dòng)裝配類(lèi)
* 根據(jù)springboot自動(dòng)裝備原理
* 將smsTemplate對(duì)象注入到容器中
* 要配置META-INF/spring.factories
*/
@Configuration
@EnableConfigurationProperties({TxProperties.class})
public class CommonsAutoConfiguration {
/**
* 注冊(cè)txSmsTemplate到容器中
* @param txProperties
* @return
*/
@Bean
public TxSmsTemplate txSmsTemplate(TxProperties txProperties) {
return new TxSmsTemplate(txProperties);
}
}
在resources中配置啟動(dòng)自動(dòng)裝配類(lèi)

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.He.commons.CommonsAutoConfiguration
最后測(cè)試一下
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");//第一個(gè)參數(shù)為手機(jī)號(hào),第二個(gè)發(fā)送短信的內(nèi)容
System.out.println(result); // result等于OK 就表示發(fā)送成功
}
}
返回OK則表示發(fā)送成功

到此這篇關(guān)于springboot整合騰訊云短信開(kāi)箱即用的文章就介紹到這了,更多相關(guān)springboot整合騰訊云短信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 中mongodb的各種操作查詢(xún)的實(shí)例詳解
這篇文章主要介紹了java 中mongodb的各種操作查詢(xún)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
SpringBoot日程管理Quartz與定時(shí)任務(wù)Task實(shí)現(xiàn)詳解
定時(shí)任務(wù)是企業(yè)級(jí)開(kāi)發(fā)中必不可少的組成部分,諸如長(zhǎng)周期業(yè)務(wù)數(shù)據(jù)的計(jì)算,例如年度報(bào)表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報(bào)告,還有搶購(gòu)類(lèi)活動(dòng)的商品上架,這些都離不開(kāi)定時(shí)任務(wù)。本節(jié)將介紹兩種不同的定時(shí)任務(wù)技術(shù)2022-09-09
Springboot通過(guò)ObjectMapper配置json序列化詳解
SpringBoot默認(rèn)集成Jackson庫(kù),其中ObjectMapper類(lèi)是核心,用于Java對(duì)象與JSON字符串的互轉(zhuǎn),提供配置序列化特性、注冊(cè)模塊等方法,在SpringBoot中可以全局配置JSON格式,如日期格式化、將Long轉(zhuǎn)為字符串,還可以配置序列化時(shí)的各種規(guī)則,感興趣的可以了解一下2024-10-10
Spring計(jì)時(shí)器stopwatch使用詳解
這篇文章主要介紹了Spring計(jì)時(shí)器stopwatch使用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Springboot通過(guò)谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能
Kaptcha是谷歌開(kāi)源的一款簡(jiǎn)單實(shí)用的圖形驗(yàn)證碼組件。我個(gè)人推薦它的最大原因是容易上手,采用約定大于配置的方式,快速契合到項(xiàng)目中,這篇文章主要介紹了Springboot通過(guò)谷歌Kaptcha組件生成圖形驗(yàn)證碼的方法,需要的朋友可以參考下2023-05-05

