腾讯云 短信
使用步骤
1、进入腾讯云搜索“短信”,进入短信控制页面
2、点击签名管理创建签名
3、点击正文模板管理,创建几个合适的短信模板
4、点击就应用管理->应用列表,创建一个应用,应用中的SDK AppID和App Key都是后面需要用到的
5、springboot项目中 pom.xml添加依赖项
<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId>
<version>1.0.6</version>
</dependency>
6、创建实体Sms,存放短信服务相关的属性
package com.shenhua.sms.test.entity;
/**
* @program: sms-test
* @Description: 发送短信必要的实体
* @Author: Akio
* @Create: 2022-05-28 11:49
**/
public class Sms {
private String phone; // 手机号
private String verification_code; // 验证码
private Integer period4validity; // 有效期
public Sms() {
}
public Sms(String phone, String verification_code, Integer period4validity) {
this.phone = phone;
this.verification_code = verification_code;
this.period4validity = period4validity;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getVerification_code() {
return verification_code;
}
public void setVerification_code(String verification_code) {
this.verification_code = verification_code;
}
public Integer getPeriod4validity() {
return period4validity;
}
public void setPeriod4validity(Integer period4validity) {
this.period4validity = period4validity;
}
}
7、对应sms短信配置写在application.properties中
server.port=8872
tencent.sms.AppID=140******
tencent.sms.AppKey=d567dd64d2101e****************
# 注意,这个SmsSignName是你的签名内容,不是ID,如果你的签名内容是中文,推荐转换为unicode编码,使用这个工具转https://tools.ijkxs.com/tools/unicode
tencent.sms.SmsSignName=\u770b\u89c1\u4f60\u4e86
tencent.sms.template.Register=141****
8、创建用于发送短信的控制器接口SmsController
package com.shenhua.sms.test.controller;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.shenhua.sms.test.entity.Sms;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
/**
* @program: sms-test
* @Description: 发送短信控制器
* @Author: Akio
* @Create: 2022-05-28 12:00
**/
@RestController
@RequestMapping(value = "/sms")
public class SmsController {
@Value("${tencent.sms.AppID}")
private Integer AppID;
@Value("${tencent.sms.AppKey}")
private String AppKey;
@Value("${tencent.sms.SmsSignName}")
private String smsSignName;
@Value("${tencent.sms.template.Register}")
private Integer template4Register;
@RequestMapping(value = "/sendRegisterCode", method = RequestMethod.POST)
public void sendCode(@RequestBody Sms sms) {
try {
// 短信中的参数,使用的模板是“您的注册验证码为{1},有效期{2}分钟,如非本人操作,请忽略本短信。”,所以填入两个参数
String[] params4sms = {sms.getVerification_code(), Integer.toString(sms.getPeriod4validity())};
SmsSingleSender smsSingleSender = new SmsSingleSender(AppID, AppKey);
SmsSingleSenderResult result = smsSingleSender.sendWithParam("86", sms.getPhone(), template4Register,
params4sms, smsSignName, "", "");
System.out.println(result);
} catch (HTTPException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
9、测试
错误码查看:https://cloud.tencent.com/document/product/382/59177