小言_互联网的博客

阿里云:发短信工具类

432人阅读  评论(0)

Maven依赖:


  
  1. <!-- 阿里云短信 -->
  2. <dependency>
  3. <groupId>com.aliyun </groupId>
  4. <artifactId>aliyun-java-sdk-core </artifactId>
  5. <version>4.1.0 </version>
  6. </dependency>

Java代码:


  
  1. package tech.yooo.ratel.common.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.aliyuncs.CommonRequest;
  4. import com.aliyuncs.CommonResponse;
  5. import com.aliyuncs.DefaultAcsClient;
  6. import com.aliyuncs.IAcsClient;
  7. import com.aliyuncs.http.MethodType;
  8. import com.aliyuncs.profile.DefaultProfile;
  9. import lombok.extern.slf4j.Slf4j;
  10. /**
  11. * 阿里云短信工具类<br>
  12. * 【1】价格说明 <br>
  13. * 验证码类:0.045元/条,如:登录验证、支付确认、登录异常等<br>
  14. * 短信通知:0.045元/条,如:物流通知、付款回执、状态通知等<br>
  15. * 推广短信:0.055元/条,如:营销推广类短信,如会员关怀、新品上线、活动通知等<br>
  16. * 国际短信:0.1元/条 - 1.2 元/条不等(有需要再添加,当前不支持该类短信)<br>
  17. *
  18. * @author ZhangYuanqiang
  19. * @since 2019/12/12
  20. */
  21. @Slf4j
  22. public class SmsUntil {
  23. private static final String ACCESS_KEY_ID = "xxxxxxxxx";
  24. private static final String ACCESS_SECRET = "yyyyyyyyy";
  25. private static final String SIGN_NAME = "哇哈哈";
  26. private static IAcsClient client;
  27. private static IAcsClient getClient() {
  28. if (client != null) {
  29. return client;
  30. } else {
  31. DefaultProfile profile =
  32. DefaultProfile.getProfile( "cn-hangzhou", ACCESS_KEY_ID, ACCESS_SECRET);
  33. client = new DefaultAcsClient(profile);
  34. }
  35. return client;
  36. }
  37. /**
  38. * 发送短信 <br>
  39. * 参考文档:https://api.aliyun.com/?spm=a2c4g.11186623.2.15.450860e2NsGyiW#/?product=Dysmsapi&api=SendSms&params={}&tab=DEMO&lang=JAVA
  40. * <br>
  41. *
  42. * @param tel 接收手机号 <br>
  43. * @param code 发送内容:只发送阿里云模板配置的变量 <br>
  44. * @param template 短信类型 {@link SmsTemplate}}<br>
  45. * @return true : 发送成功,false : 发送失败
  46. */
  47. public static boolean sendVerifyCode(String tel, String code, String template) {
  48. log.info( "sendVerifyCode: 发送验证码 [tel:{}, code:{}, template:{}]", tel, code, template);
  49. CommonRequest request = getCommonRequest( "SendSms");
  50. request.putQueryParameter( "TemplateCode", template);
  51. request.putQueryParameter( "PhoneNumbers", tel);
  52. request.putQueryParameter( "TemplateParam", String.format( "{\"code\":\"%s\"}", code));
  53. try {
  54. CommonResponse response = getClient().getCommonResponse(request);
  55. String resp = response.getData();
  56. JSONObject obj = JSONObject.parseObject(resp);
  57. String message = obj.getString( "Message");
  58. if (message.equals( "OK")) {
  59. log.info( "sendVerifyCode: 发送成功!");
  60. return true;
  61. } else {
  62. log.info( "sendVerifyCode: 发送失败![{}]", resp);
  63. return false;
  64. }
  65. } catch (Exception e) {
  66. log.info( "sendVerifyCode: 发送验证码异常");
  67. e.printStackTrace();
  68. return false;
  69. }
  70. }
  71. private static CommonRequest getCommonRequest(String action) {
  72. CommonRequest request = new CommonRequest();
  73. request.setMethod(MethodType.POST);
  74. request.setDomain( "dysmsapi.aliyuncs.com");
  75. request.setVersion( "2017-05-25");
  76. request.setAction(action);
  77. request.putQueryParameter( "RegionId", "cn-hangzhou");
  78. request.putQueryParameter( "SignName", SIGN_NAME);
  79. return request;
  80. }
  81. public static class SmsTemplate {
  82. /** 身份验证验证码 */
  83. public static String AUTHENTICATION = "SMS_178535388";
  84. /** 登录确认验证码 */
  85. public static String LOGIN_CONFIRM = "SMS_178535387";
  86. /** 登录异常验证码 */
  87. public static String LOGIN_UNEXPECTED = "SMS_178535386";
  88. /** 用户注册验证码 */
  89. public static String REGISTER = "SMS_178535385";
  90. /** 修改密码验证码 */
  91. public static String UPDATE_PASSWORD = "SMS_178535384";
  92. /** 信息变更验证码 */
  93. public static String INFO_CHANGE = "SMS_178535383";
  94. }
  95. }

 


转载:https://blog.csdn.net/sunnyzyq/article/details/103517144
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场