一、引入依赖

  1. <!-- 阿里云短信服务-->
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-core</artifactId>
  5. <version>4.5.17</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.aliyun</groupId>
  9. <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
  10. <version>2.2.1</version>
  11. </dependency>

二、创建工具类

  1. @Service
  2. @Slf4j
  3. public class EpidemicMsmUtils implements EpidemicMsm {
  4. private static String endpoint = "cn-hangzhou";
  5. private static String accessKeyId = "LTAI5tDvuHomL95pvrQSWYsH";
  6. private static String accessSecret = "fZovzcSrpLIY4AHpbWPVwRCIsWi1gl";
  7. /**
  8. * 发送短信验证码
  9. * @param param 验证码
  10. * @param phone 手机号
  11. * @return
  12. */
  13. @Override
  14. public boolean send(String content, String phone){
  15. DefaultProfile profile = DefaultProfile.getProfile(endpoint,accessKeyId,accessSecret);
  16. IAcsClient client = new DefaultAcsClient(profile);
  17. CommonRequest request = new CommonRequest();
  18. request.setSysMethod(MethodType.POST);
  19. //域名,勿改
  20. request.setSysDomain("dysmsapi.aliyuncs.com");
  21. //API版本号,请勿修改
  22. request.setSysVersion("2017-05-25");
  23. //API名称
  24. request.setSysAction("SendSms");
  25. //接收号码,格式为:号码,必填
  26. request.putQueryParameter("PhoneNumbers", phone);
  27. //申请阿里云 签名名称
  28. request.putQueryParameter("SignName","阿里云短信测试");
  29. //申请阿里云 模板code SMS_ 219890080
  30. request.putQueryParameter("TemplateCode","SMS_154950909");
  31. // 此处是进行验证码短信参数设置
  32. Map<String, Object> param = new HashMap<>();
  33. param.put("code", content);
  34. //短信内容,必填
  35. request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
  36. try {
  37. CommonResponse response = client.getCommonResponse(request);
  38. return response.getHttpResponse().isSuccess();
  39. } catch (ClientException e) {
  40. e.printStackTrace();
  41. return false;
  42. }
  43. }
  44. /**
  45. * 批量发送短信
  46. * @param content 短信内容
  47. * @param phone 手机号
  48. * @return
  49. */
  50. @Override
  51. public boolean sendBatch(String content, String[] phone){
  52. DefaultProfile profile = DefaultProfile.getProfile(endpoint,accessKeyId,accessSecret);
  53. IAcsClient client = new DefaultAcsClient(profile);
  54. CommonRequest request = new CommonRequest();
  55. request.setSysMethod(MethodType.POST);
  56. //域名,勿改
  57. request.setSysDomain("dysmsapi.aliyuncs.com");
  58. //API版本号,请勿修改
  59. request.setSysVersion("2017-05-25");
  60. //API名称
  61. request.setSysAction("SendBatchSms");
  62. //接收号码,格式为:国际码+号码,必填
  63. request.putQueryParameter("PhoneNumberJson", Arrays.toString(phone));
  64. //申请阿里云 签名名称
  65. request.putQueryParameter("SignNameJson","阿里云短信测试");
  66. //申请阿里云 模板code
  67. request.putQueryParameter("TemplateCode","SMS_154950909");
  68. //短信内容,必填
  69. request.putQueryParameter("TemplateParamJson", JSONObject.toJSONString(content));
  70. try {
  71. CommonResponse response = client.getCommonResponse(request);
  72. return response.getHttpResponse().isSuccess();
  73. } catch (ClientException e) {
  74. e.printStackTrace();
  75. return false;
  76. }
  77. }
  78. }