發送短信驗證碼---阿里大於

使用阿里大於發送短信

1.進入阿里雲控制檯,搜索 ‘短信服務’

在這裏插入圖片描述

2.點擊’快速學習’,界面如下

需要簽名和模板,這也是調用發送短信的關鍵
在這裏插入圖片描述

3.點擊國內消息和添加簽名

在這裏插入圖片描述
在這裏插入圖片描述
完成操作後:
在這裏插入圖片描述
主要需要兩個參數:ID和Secret
在這裏插入圖片描述
由於我已經申請了一個驗證碼了,現在看到的是申請通用的界面,就不做介紹了…
有疑問可以參考https://www.cnblogs.com/amunamuna/p/9686661.html

4.阿里大於發送短信工具類

<!--手機短信依賴-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.6</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.6</version>
        </dependency>
package com.athome.ali;

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;

/**
 * @author liang
 * @version 1.0
 * @date 2020/3/30 20:28
 */
public class AliDaYuUtil {
    /**
     * 根據用戶手機號發送短信驗證碼
     * @param phoneNumbers : 短信收件人
     * @param length : 驗證碼長度
     * @return : 短信驗證碼code
     */
    public static String sendTelValidateCode(String phoneNumbers ,int length ){
        /**
         * 調用短信API請求
         *
         * access key
         * regionId:短信服務區域.
         * accessKeyId:阿里雲短信服務access keyId.
         * secret:access key密鑰.
         */
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4Fpfk3f2bfeMdMnpeToN", "ICAlFtny2ABNpVgGeWd49O8ftdHwBV");
        IAcsClient client = new DefaultAcsClient(profile);
        /**
         * 傳入參數,調用SDK
         */
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        /**
         * 短信通知用戶
         */
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        /**
         * 短信簽名
         */
        request.putQueryParameter("SignName", "巔峯");//巔峯
        /**
         * 短信通知模板
         */
        request.putQueryParameter("TemplateCode", "SMS_186951578");//SMS_186951578:驗證碼; SMS_186946660:短信驗證
        /**
         * 模板變量名
         * 用戶姓名name,時段time,數值code
         */
        String code =StringUtil.getDigitalRandom(length);
        int i = Integer.parseInt(code);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+i+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            if (!response.getData().contains("OK")){
                return null ;
            }
        }
        catch (ServerException e) {
            e.printStackTrace();
            return null ;
        }
        catch (ClientException e) {
            e.printStackTrace();
            return null ;
        }
        return  code ;
    }

    /**
     * 發送短信通知,修改密碼成功等。。。
     * @param tel : 手機號
     * @return
     */
    public static String sendTelMessage(String tel ){
        return null ;
    }

    public static void main(String[] args) {
        String s = sendTelValidateCode("15984251717", 6);
        System.out.println(s);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章