java阿里云发送短信

java阿里云发送短信,第1张

java阿里云发送短信

pom


    com.aliyun
    aliyun-java-sdk-core
    4.5.16

实现方法

public String generateAuthCode(String telephone) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for(int i=0;i<6;i++){
            sb.append(random.nextInt(10));
        }
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "ccessKeyId", "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("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", "公司名");
        request.putQueryParameter("TemplateCode", "");
        // 构建一个短信
        HashMap hashMap = new HashMap<>();
        hashMap.put("code", sb.toString());
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(hashMap));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return sb.toString();
}
~有写错的或者有更优雅的写法记得@ 我~

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5574838.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存