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();
            }
        }
    }
    
  • 相关阅读:
    一致性哈希算法
    Discourse 的标签(Tag)只能是小写的原因
    JIRA 链接 bitbucket 提示错误 Invalid OAuth credentials
    JIRA 如何连接到云平台的 bitbucket
    Apache Druid 能够支持即席查询
    如何在 Discourse 中配置使用 GitHub 登录和创建用户
    Apache Druid 是什么
    Xshell 如何导入 PuTTYgen 生成的 key
    windows下配置Nginx支持php
    laravel连接数据库提示mysql_connect() :Connection refused...
  • 原文地址:https://www.cnblogs.com/to-red/p/12450598.html
Copyright © 2011-2022 走看看