使用腾讯云sms短信服务

因为使用gradle管理springboot,本文需用到旧版腾讯云SDK资源:传送门
如果使用maven,新版的腾讯云SDK可以很方便引用,后面会提到SDK。

1、注册微信公众号

百度搜索微信公众号,进入网站后,点注册,注册类型选订阅号(个人)。注册成功后保存后台“账号详情”的截图,用于后面申请腾讯云短信签名。

2、注册腾讯云账号

使用qq号登录注册腾讯云,完成个人实名认证流程。

3、设置短信签名和短信正文

进入腾讯云后台,搜索框内搜“短信”,确认开通。

在短信管理后台里,找到SDK,下载Java SDK。用于后期开发。(新版SDK对gradle不友好,建议使用上面旧版下载)

先申请签名,等待审核通过后再申请短信正文。请添加图片描述
申请短信正文模板:
在这里插入图片描述

4、在gradle demo项目中测试

先把旧版核心包qcloudsms-1.0.5.jar导入到bin文件夹下,然后build path构建一下:
在这里插入图片描述
创建对应包下的测试类:com.zzz.ssjpa.controller.UserController(包结构看前面关于springboot文章),代码如下:

package com.zzz.ssjpa.controller;

import java.io.IOException;

import org.json.JSONException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;

@RestController
public class UserController {

	/**
	 * @Title: test   
	 * @Description: 给手机发送短信  
	 * @param: @return 参数
	 * @return: String 返回类型
	 * @throws
	 */
	@RequestMapping("/test")
	public String test() {
		
		//appid  appkey
		int appid = xxx;
		String appkey = "xxx";
		//短信模板id
		int templateId = xxx;
		//签名的名字
		String smsSign = "玄尺软件编程公众号";
		
		//给谁发
		String phoneNumber = "xxx";
		//验证码
		String[] params = {"1234"};
		
		SmsSingleSender sender = new SmsSingleSender(appid, appkey);
		//地区、电话、模板id、验证码、签名
		try {
			SmsSingleSenderResult result = sender.sendWithParam("86", phoneNumber, templateId, params, smsSign, "", "");
			System.out.println(result);
		} catch (HTTPException | JSONException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "success";
	}
}

7、运行主程序测试

手机收到验证码就表示成功了。Good luck。

Java使用腾讯云发送短信服务,需要进行以下步骤: 1. 注册腾讯云账号,并开通短信服务。 2. 在腾讯云控制台中,创建应用和签名模板。应用表示您的业务场景,签名模板表示发送短信的签名和模板内容。 3. 在Java项目中引入腾讯云SDK,可以通过Maven依赖来引入。 4. 创建短信发送API,并调用腾讯云SDK提供的发送短信接口。 下面是一个简单的示例代码,可以参考: ``` import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.sms.v20190711.SmsClient; import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest; import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse; public class TencentSmsSender { public static void main(String[] args) { String secretId = "your_secret_id"; String secretKey = "your_secret_key"; String appid = "your_appid"; String sign = "your_sign"; // 签名名称 String templateId = "your_template_id"; // 模板 ID try { Credential cred = new Credential(secretId, secretKey); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("sms.tencentcloudapi.com"); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); SmsClient client = new SmsClient(cred, "", clientProfile); SendSmsRequest req = new SendSmsRequest(); req.setSmsSdkAppid(appid); req.setSign(sign); req.setTemplateID(templateId); req.setPhoneNumberSet(new String[]{"+86138xxxx0000"}); // 接收短信的手机号码 req.setTemplateParamSet(new String[]{"code=123456"}); // 短信模板中的参数 SendSmsResponse res = client.SendSms(req); System.out.println(SendSmsResponse.toJsonString(res)); } catch (TencentCloudSDKException e) { e.printStackTrace(); } } } ``` 需要注意的是,腾讯云短信服务需要进行实名认证才能使用。另外,短信发送需要消耗一定的费用,具体费用可以在腾讯云控制台中查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值