Java中通过腾讯云发送短信主要涉及以下几个基础概念:
以下是一个简单的Java示例,展示如何使用腾讯云SDK发送短信:
import com.tencentcloudapi.common.Credential;
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 SmsSender {
public static void main(String[] args) {
try {
// 实例化一个认证对象,入参需要传入腾讯云账户的SecretId和SecretKey
Credential cred = new Credential("你的SecretId", "你的SecretKey");
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("sms.tencentcloudapi.com");
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 实例化要请求产品的client对象,clientProfile是可选的
SmsClient client = new SmsClient(cred, "", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
SendSmsRequest req = new SendSmsRequest();
req.setSmsSdkAppId("你的SmsSdkAppId");
req.setSignName("你的短信签名");
req.setTemplateId("你的短信模板ID");
req.setTemplateParamSet(new String[]{"参数1", "参数2"});
req.setPhoneNumberSet(new String[]{"接收短信的手机号码"});
// 通过client对象调用想要访问的接口,需要传入请求对象
SendSmsResponse resp = client.SendSms(req);
// 输出json格式的字符串回包
System.out.println(SendSmsResponse.toJsonString(resp));
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
希望以上信息对你有所帮助!
没有搜到相关的文章