腾讯云配置
创建签名
创建短信模板
创建应用
创建访问密钥
java实现
导包
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId>
<version>3.1.715</version>
</dependency>
简洁版
package com.example.demo.utils;
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.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
public class SMSUtil {
// 访问ID
private static final String id = "AKIDNdkgR2NYaSRYv9JLDwVq10mMl176rVzk";
// 访问密钥
private static final String key = "eCg2BD5mHbBaoriNo692ObbZRH1b5NXV";
// 短信应用sdk,需要申请
private static final String appid = "1400802780";
// 模板ID,需要申请
private static final String templateId = "1731567";
// 签名ID,需要申请
private static final String smsSign = "一个破水瓶公众号";
/**
*
* @param templateParamSet 模板参数,需要腾讯云定义
* @param phoneNumbers 需要发短信的手机号
*/
public static void sendTextMessage(String[] templateParamSet, String[] phoneNumbers) {
try {
Credential cred = new Credential(id, key);
// 实例化一个http选项,可选,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
/* SDK默认使用POST方法,可以设置为get,但是无法处理较大的请求*/
httpProfile.setReqMethod("POST");
/* SDK有默认的超时时间 */
httpProfile.setConnTimeout(60);
/* 指定接入地域域名 */
httpProfile.setEndpoint("sms.tencentcloudapi.com");
/* 实例化一个客户端配置对象,可以指定超时时间等配置 */
ClientProfile clientProfile = new ClientProfile();
/* SDK默认用TC3-HMAC-SHA256进行签名*/
clientProfile.setSignMethod("HmacSHA256");
clientProfile.setHttpProfile(httpProfile);
/* 实例化要请求产品(以sms为例)的client对象 第二个参数是地域信息 */
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
// 设置发送的信息与基本配置
SendSmsRequest req = new SendSmsRequest();
/* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
req.setSmsSdkAppId(appid);
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
req.setSignName(smsSign);
/* 模板 ID: 必须填写已审核通过的模板 ID */
req.setTemplateId(templateId);
/* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
req.setTemplateParamSet(templateParamSet);
/* 下发手机号码,最多不要超过200个手机号 */
req.setPhoneNumberSet(phoneNumbers);
/* 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
SendSmsResponse res = client.SendSms(req);
// 输出json格式的字符串回包
System.out.println(SendSmsResponse.toJsonString(res));
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
}
}
全文
package com.example.demo.utils;
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.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
public class SMSUtil {
// 访问ID
private static final String id = "AKIDNdkgR0NYaSRYv9JLDwVq10mMl174rVzk";
// 访问密钥
private static final String key = "eCg2BD5mHbBaoriNo692ObbZRH1b5NXV";
// 短信应用sdk,需要申请
private static final String appid = "1400802780";
// 模板ID,需要申请
private static final String templateId = "1731567";
// 签名ID,需要申请
private static final String smsSign = "一个破水瓶公众号";
/**
*
* @param templateParamSet 模板参数,需要腾讯云定义
* @param phoneNumbers 需要发短信的手机号
*/
public static void sendTextMessage(String[] templateParamSet, String[] phoneNumbers) {
try {
Credential cred = new Credential(id, key);
// 实例化一个http选项,可选,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
// 设置代理(无需要直接忽略)
// httpProfile.setProxyHost("真实代理ip");
// httpProfile.setProxyPort(真实代理端口);
/* SDK默认使用POST方法。
* 如果你一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
httpProfile.setReqMethod("POST");
/* SDK有默认的超时时间,非必要请不要进行调整
* 如有需要请在代码中查阅以获取最新的默认值 */
httpProfile.setConnTimeout(60);
/* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com */
httpProfile.setEndpoint("sms.tencentcloudapi.com");
/* 非必要步骤:
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
ClientProfile clientProfile = new ClientProfile();
/* SDK默认用TC3-HMAC-SHA256进行签名
* 非必要请不要修改这个字段 */
clientProfile.setSignMethod("HmacSHA256");
clientProfile.setHttpProfile(httpProfile);
/* 实例化要请求产品(以sms为例)的client对象
* 第二个参数是地域信息 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 */
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
/* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
* 你可以直接查询SDK源码确定接口有哪些属性可以设置
* 属性可能是基本类型,也可能引用了另一个数据结构
* 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
SendSmsRequest req = new SendSmsRequest();
/* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
req.setSmsSdkAppId(appid);
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
// 签名信息
req.setSignName(smsSign);
/* 模板 ID: 必须填写已审核通过的模板 ID */
req.setTemplateId(templateId);
/* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
req.setTemplateParamSet(templateParamSet);
/* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
* 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 */
req.setPhoneNumberSet(phoneNumbers);
/* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
String sessionContext = "";
req.setSessionContext(sessionContext);
/* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
String extendCode = "";
req.setExtendCode(extendCode);
/* 国际/港澳台短信 SenderId(无需要可忽略): 国内短信填空,默认未开通,如需开通请联系 [腾讯云短信小助手] */
String senderid = "";
req.setSenderId(senderid);
/* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
* 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
SendSmsResponse res = client.SendSms(req);
// 输出json格式的字符串回包
System.out.println(SendSmsResponse.toJsonString(res));
// 也可以取出单个值,你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
// System.out.println(res.getRequestId());
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
}
}