package com.care.common.util; 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; /** * @Author: lilt * @Date: 2021/6/5 * @Desc: */ 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{ 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); System.out.println(JSONUtil.toJsonStr(resp)); sendStatus = resp.getSendStatusSet()[0]; } catch (TencentCloudSDKException e) { System.out.println(e.toString()); } return sendStatus; } public static void main(String[] args) { String SecretId = "AKIDkKfkbCX0HJ4YkgMlunPkpdBSVHo43mEQ"; String SecretKey = "wb08zZrRkmY3IGyPZS4PUIp66pHUQ6Vd"; String phoneNumber = "18010375763"; String SmsSdkAppId = "1400531694"; String signName ="熵行科技"; String templateId ="1097265"; String[] templateParam = {"北太平庄201"}; sendSms("sms.tencentcloudapi.com","ap-beijing",SecretId,SecretKey,SmsSdkAppId,signName,templateId,phoneNumber,templateParam); } }