SmsUtil.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.care.common.util;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.json.JSONUtil;
  4. import com.tencentcloudapi.common.Credential;
  5. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  6. import com.tencentcloudapi.common.profile.ClientProfile;
  7. import com.tencentcloudapi.common.profile.HttpProfile;
  8. import com.tencentcloudapi.sms.v20210111.SmsClient;
  9. import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
  10. import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
  11. import com.tencentcloudapi.sms.v20210111.models.SendStatus;
  12. import lombok.extern.slf4j.Slf4j;
  13. /**
  14. * @Author: lilt
  15. * @Date: 2021/6/5
  16. * @Desc:
  17. */
  18. @Slf4j
  19. public class SmsUtil {
  20. public static SendStatus sendSms(String endpoint,
  21. String region,
  22. String secretId,
  23. String secretKey,
  24. String smsSdkAppId,
  25. String signName,
  26. String templateId,
  27. String phoneNumber,
  28. String[] templateParam){
  29. SendStatus sendStatus = null;
  30. try{
  31. if (StrUtil.isEmpty(phoneNumber)){
  32. return sendStatus;
  33. }
  34. Credential cred = new Credential(secretId, secretKey);
  35. HttpProfile httpProfile = new HttpProfile();
  36. httpProfile.setEndpoint(endpoint);
  37. ClientProfile clientProfile = new ClientProfile();
  38. clientProfile.setHttpProfile(httpProfile);
  39. SmsClient client = new SmsClient(cred, region, clientProfile);
  40. if (phoneNumber.startsWith("+86")){
  41. //phoneNumber = phoneNumber.replace("+86","");
  42. }else{
  43. phoneNumber = "+86"+phoneNumber;
  44. }
  45. String[] phoneNumberSet = {phoneNumber};
  46. SendSmsRequest req = new SendSmsRequest();
  47. req.setPhoneNumberSet(phoneNumberSet);
  48. req.setSmsSdkAppId(smsSdkAppId);
  49. req.setSignName(signName);
  50. req.setTemplateId(templateId);
  51. req.setTemplateParamSet(templateParam);
  52. SendSmsResponse resp = client.SendSms(req);
  53. log.info("短信发送:{},{}",phoneNumber,JSONUtil.toJsonStr(resp));
  54. sendStatus = resp.getSendStatusSet()[0];
  55. } catch (TencentCloudSDKException e) {
  56. log.error("短信发送失败:{},{}",phoneNumber,e);
  57. }
  58. return sendStatus;
  59. }
  60. public static void main(String[] args) {
  61. String SecretId = "AKIDkKfkbCX0HJ4YkgMlunPkpdBSVHo43mEQ";
  62. String SecretKey = "wb08zZrRkmY3IGyPZS4PUIp66pHUQ6Vd";
  63. String phoneNumber = "17896089601";
  64. String SmsSdkAppId = "1400531694";
  65. String signName ="熵行科技";
  66. String templateId ="1097265";
  67. String[] templateParam = {"北太平庄201"};
  68. sendSms("sms.tencentcloudapi.com","ap-beijing",SecretId,SecretKey,SmsSdkAppId,signName,templateId,phoneNumber,templateParam);
  69. }
  70. }