实现起来都是比较简单的!麻烦在配置开通等等
1. 流程
(可能有成本)
我们平常发手机短信,首先我们得有自己的手机号,然后输入目标手机号,然后进行发送短信
那其实我们首先登录注册时想要获取验证码,就类似于我们用一个手机发送短信给目标手机号一样;我们后面就是要去腾讯云那边开通一个短信服务,就相当于有一个手机可以发短信,我们只需要调用sdk方法让他去发短信!
(零成本)
我们平常发送qq邮箱,也是要有自己的邮箱,然后输入目标邮箱,然后发送邮箱信息
同理我们需要在我们的qq邮箱去开启服务(主要就是来链接qq那边的接口,然后发送短信服务)

2. 腾讯云短信验证码注册
腾讯云短信服务实现 Java 发送手机验证码(SpringBoot+Redis 实现)-腾讯云开发者社区-腾讯云
2.1. 腾讯云短信服务
首先我们想要使用给腾讯云的短信服务需要上传很多资料的,阿里云的短信服务也是如此!
2.2. 实名资质管理:

2.3. 签名管理:

可以去弄一个简单的公众号!
2.4. 正文模板管理:
主要是去设置你的短信内容发送的模板样式

2.5. 审核完毕所有内容后:


2.6. 复制好你之前的签名模板等id(有用)
比如:

2.7. 同时需要密钥
所以需要注意创建时保留好SecretId和 SecretKey

2.8. SDKAppID
https://console.cloud.tencent.com/smsv2/app-manage

2.9. SDK实现——maven
2.9.1. 引入依赖包
<!-- 腾讯云手机号验证码依赖包 -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
<version>3.1.1000</version>
</dependency>
如果下载的慢的话:


2.9.2. 代码复制
只需要填写相关信息就可以使用了
简化版:
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
public class TencentSmsSender {
public void sendSms(String phoneNumber, String verifyCode) throws TencentCloudSDKException {
String secretId = "你的腾讯云API密钥-SecretId";
String secretKey = "你的腾讯云API密钥-SecretKey";
String sdkAppId = "你的腾讯云短信App ID"; // 例如:1400006666
String signName = "你的短信签名内容"; // 例如:"腾讯云短信测试"
String templateId = "你的短信模板ID"; // 例如:"1176554"
Credential cred = new Credential(secretId, secretKey);
SmsClient client = new SmsClient(cred, "ap-guangzhou"); // 选择广州地区
SendSmsRequest req = new SendSmsRequest();
req.setSmsSdkAppid(sdkAppId);
req.setSign(signName);
req.setTemplateID(templateId);
req.setPhoneNumberSet(phoneNumber);
req.setTemplateParamSet(verifyCode);
SendSmsResponse resp = client.SendSms(req);
// 输出返回的结果
System.out.println(SendSmsResponse.toJsonString(resp));
}
public static void main(String[] args) {
try {
TencentSmsSender sender = new TencentSmsSender();
sender.sendSms("收信人手机号", "验证码内容");
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
}
}
完整版
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
//导入可选配置类
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
// 导入对应SMS模块的client
import com.tencentcloudapi.sms.v20210111.SmsClient;
// 导入要请求接口对应的request response类
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
/**
* Tencent Cloud Sms Sendsms
*
*/
public class SendSms
{
public static void main(String[] args)
{
try {
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId,SecretKey。
// 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中,请参考凭证管理 https://github.com/TencentCloud/tencentcloud-sdk-java?tab=readme-ov-file#%E5%87%AD%E8%AF%81%E7%AE%A1%E7%90%86。
// 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
// SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi
// Credential cred = new Credential("SecretId", "SecretKey");
Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
// 从3.0.96版本开始, 单独设置 HTTP 代理(无需要直接忽略)
// httpProfile.setProxyHost("真实代理ip");
// httpProfile.setProxyPort(真实代理端口);

最低0.47元/天 解锁文章

696

被折叠的 条评论
为什么被折叠?



