zoukankan      html  css  js  c++  java
  • 使用阿里云短信服务

    SpringBoot中使用阿里云的短信服务。

    ​ 很早之前我就想使用短信服务。浏览过阿里云和腾讯云的短信服务,看到5000条一年,一万条一年的套餐,只是平时的测试和自己使用,完全用不了这么多。我天真的认为,一定要买套餐才能使用。

    ​ 后来才了解到,只要有阿里云的账号,就能直接使用短信服务,费用也只要5分一条。腾讯云我之前测试的时候,直接复制官网的demo就能跑。

    ​ 但是阿里云的短信服务,使用官方提供的demo,不能直接使用。在此做个记录。

    ​ 阿里云的提供的SDK的pom.xml

    <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.0.3</version>
    </dependency>
    <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
                <version>2.1.0</version>
    </dependency>
    

    官方的DEMO版本有些问题,部分方法对不上,需要小小的修改。官方的DEMO和修改如下:

    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.profile.DefaultProfile;
    
    public class SendSms {
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);// 官方为request.setSysMethod
            request.setDomain("dysmsapi.aliyuncs.com");// 官方为request.setSysDomain
            request.setVersion("2017-05-25"); // 官方为request.setSysVersion
            request.setAction("SendSms");// 官方为request.setSysAction
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", "1.....02");
            request.putQueryParameter("SignName", "ho...");
            request.putQueryParameter("TemplateCode", "SMS_18266...");
            request.putQueryParameter("TemplateParam", "{"code":"1234"}");
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    springboot整合mybatis
    nginx
    mysql安装
    oracle安装静默
    网卡
    kafak部署
    在docker环境下安装activemq和rabbitmq
    docker安装
    【1213工作日志】ZYNQ的中断应用
    【CAN总线】CAN总线总结
  • 原文地址:https://www.cnblogs.com/to-red/p/12450598.html
Copyright © 2011-2022 走看看