码迷,mamicode.com
首页 > 其他好文 > 详细

阿里 短信认证

时间:2019-02-13 18:09:20      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:失败   span   rgs   void   efault   template   dom   rop   名称   

package com.yetech;

import java.util.Random;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SignDemo {
    
    static final String product = "Dysmsapi";//产品名称
    static final String domain = "dysmsapi.aliyuncs.com";//产品域名
    
    public static SendSmsResponse sendSms(String phone, String code, String accessKeyId, String accessKeySecret) throws ClientException {
        //可自助调整超时时间10s
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        
        //初始化acsClient
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        
        //组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        //必填
        request.setPhoneNumbers(phone);//待发送手机号
        request.setSignName("");//自己定义的短信签名
        request.setTemplateCode("");//自己定义的短信模板
        //可选
        request.setTemplateParam("{\"code\":\""+code+"\"}");
        SendSmsResponse sendSmsResponse = null;
        try {
            sendSmsResponse = acsClient.getAcsResponse(request); 
            System.out.println(sendSmsResponse.getMessage() + "------" + sendSmsResponse.getRequestId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
            System.out.println("短信发送成功!");
        }else {
                System.out.println("短信发送失败!");
        }
        return sendSmsResponse;
    }
    
    public static void main(String[] args) {
        try {
            Random random = new Random();
            String code = String.valueOf(random.nextInt(9000) + 1000);
            SignDemo.sendSms("15270170013",code,"你的accessKeyId","你的accessKeySecret");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    
}

需要的阿里jar包

链接:https://pan.baidu.com/s/1wuA3ILwZwp7KD4Bg0n4NWA 

提取码:nsuf

阿里 短信认证

标签:失败   span   rgs   void   efault   template   dom   rop   名称   

原文地址:https://www.cnblogs.com/ch94/p/10371219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!