SmsUtil.java 2.9 KB

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