12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package com.care.common.util;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.json.JSONUtil;
- 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;
- import com.tencentcloudapi.sms.v20210111.models.SendStatus;
- import lombok.extern.slf4j.Slf4j;
- /**
- * @Author: lilt
- * @Date: 2021/6/5
- * @Desc:
- */
- @Slf4j
- public class SmsUtil {
- public static SendStatus sendSms(String endpoint,
- String region,
- String secretId,
- String secretKey,
- String smsSdkAppId,
- String signName,
- String templateId,
- String phoneNumber,
- String[] templateParam){
- SendStatus sendStatus = null;
- try{
- if (StrUtil.isEmpty(phoneNumber)){
- return sendStatus;
- }
- Credential cred = new Credential(secretId, secretKey);
- HttpProfile httpProfile = new HttpProfile();
- httpProfile.setEndpoint(endpoint);
- ClientProfile clientProfile = new ClientProfile();
- clientProfile.setHttpProfile(httpProfile);
- SmsClient client = new SmsClient(cred, region, clientProfile);
- if (phoneNumber.startsWith("+86")){
- //phoneNumber = phoneNumber.replace("+86","");
- }else{
- phoneNumber = "+86"+phoneNumber;
- }
- String[] phoneNumberSet = {phoneNumber};
- SendSmsRequest req = new SendSmsRequest();
- req.setPhoneNumberSet(phoneNumberSet);
- req.setSmsSdkAppId(smsSdkAppId);
- req.setSignName(signName);
- req.setTemplateId(templateId);
- req.setTemplateParamSet(templateParam);
- SendSmsResponse resp = client.SendSms(req);
- log.info("短信发送:{},{}",phoneNumber,JSONUtil.toJsonStr(resp));
- sendStatus = resp.getSendStatusSet()[0];
- } catch (TencentCloudSDKException e) {
- log.error("短信发送失败:{},{}",phoneNumber,e);
- }
- return sendStatus;
- }
- public static void main(String[] args) {
- String SecretId = "AKIDkKfkbCX0HJ4YkgMlunPkpdBSVHo43mEQ";
- String SecretKey = "wb08zZrRkmY3IGyPZS4PUIp66pHUQ6Vd";
- String phoneNumber = "17896089601";
- String SmsSdkAppId = "1400531694";
- String signName ="熵行科技";
- String templateId ="1097265";
- String[] templateParam = {"北太平庄201"};
- sendSms("sms.tencentcloudapi.com","ap-beijing",SecretId,SecretKey,SmsSdkAppId,signName,templateId,phoneNumber,templateParam);
- }
- }
|