阿里云短信接码

发布于 2022-08-12  88 次阅读


步骤

登录阿里云 - 开通短信服务 - 获取 accessKeyID - 申请签名 - 申请短信模板 - 发送短信

file

申请签名
file

申请短信模板
file

依赖

<dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.7</version>
        </dependency>

配置类

/** access key */
public final static String accessKeyID = "LTA**********bhp";
/** secret key */
public final static String accessKeySecret = "RV********CNk";
//  地域
public final static String aliAddress = "cn-hangzhou";
// 短信签名名称
public final static String signName ="*****";
// 短信模板
public final static String templateCode = "SMS_******13";

短信工具类

import java.util.Random;
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;
import com.zfyy.sign.config.Config;

public class SampleUtil {
    /**
     * @param phone
     *            发送到的手机号
     * @param code
     *            验证码内容
     */
    public static void sendCode(String phone, String code) {
        //此处放AccessKeyID和AccessKeySecret
        DefaultProfile profile = DefaultProfile.getProfile(Config.aliAddress, Config.accessKeyID,Config.accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", Config.aliAddress);
        //此处放接收验证码的手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //此处放签名名称(必须审核通过)
        request.putQueryParameter("SignName", Config.signName);
        //此处放短信模板(必须审核通过)
        request.putQueryParameter("TemplateCode", Config.templateCode);
        //此处放验证码的内容(JSON格式\"表示转义。JSON格式:{"code":"666666"})
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return 随机生成的6位验证码
     */
    public static String getCode() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; sb.length() < 6; i++) {
            int num = new Random().nextInt(10);
            sb.append(num);
        }
        return sb.toString();
    }
}

测试

@Test
    public void test() {
        String code = SampleUtil.getCode();
        String phone = "173****3390";
        SampleUtil.sendCode(phone, code);
    }

结果

{"Message":"OK","RequestId":"B3CB622B-DF8D-55A6-9E4B-39D49C30FEEF","Code":"OK","BizId":"276219260287515180^0"}

本当の声を響かせてよ