阿里云短信服务从0到1

阿里云-计算,为了无法计算的价值

搜索短信,开通服务

选择国内消息,资质管理——新增资质

签名管理——添加签名,签名添加后不能修改,所以认真查看,来源最好选企事业单位,名称最好是企业全名称,前段时间因为响应工信部签名报备,移动号码发送不了短信

模板管理——添加模板,可以编写发送内容模板,字数有限制,70个字按照一条短信收费,超过之后按照67个字一条短信收费

审核都通过后可以购买短信条数,在概览——购买。新用户应该会送几条给你调试

导入依赖

<dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibabacloud-dysmsapi20170525</artifactId>
            <version>2.0.24</version>
        </dependency>

发送验证,phone:发送手机号,code:验证码,验证码随机生成一个缓存下来,需要与用户输入的进行比对

public static boolean sendAliyun(String phone,String code) throws Exception {

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                .accessKeyId("你的keyid")
                .accessKeySecret("你的key密钥")
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();

        // Parameter settings for API request
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",code);
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phone)
                .signName("签名名称")
                .templateCode("模板code")
                .templateParam(jsonObject.toJSONString())
                // Request-level configuration rewrite, can set Http request parameters, etc.
                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        // Synchronously get the return value of the API request
        SendSmsResponse resp = response.get();

        LogUtil.info(new Gson().toJson(resp));

        client.close();
        return "OK".equalsIgnoreCase(resp.getBody().getCode()) && "OK".equalsIgnoreCase(resp.getBody().getMessage());
    }

获取keyid和keysecret

进入accesskey中,创建一个就行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值