阿里最新版短信工具类

其实是一个简单的工具类,之前用旧版本的,总是在项目启动的时候报错,然后看到阿里有更新的SDK,然后就使用了,接下来就报了一个无缘无故的错误 "Exception in thread "main" java.lang.NoSuchMethodError: org.json.JSONArray.i"   然后发现是阿里官方文档提供的 aliyun-java-sdk-core 版本过低  ,改成4.0.6就没问题了,


            com.aliyun
            aliyun-java-sdk-core
            4.0.6


            com.aliyun
            aliyun-java-sdk-ecs
            4.11.0
 

public class SMSUtils {

    private static Logger logger = LoggerFactory.getLogger(SMSUtils.class);

    /**
     * 阿里云发送短信
     * @param templateId 手机模板id
     * @param mobile 手机号码
     * @param code 验证码
     * @param type 短信验证业务类型
     * @return 0 发送成功  -1发送失败
     * 
     */
    public static int aliSendCode(String templateId, String mobile, String code) {
        int i = 0;
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", Params.accessKeyId, Params.accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        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");
        //区域代码---"cn-hangzhou"
        request.putQueryParameter("RegionId", "cn-hangzhou");
        //手机号码
        request.putQueryParameter("PhoneNumbers", mobile);
        //签名名称
        request.putQueryParameter("SignName", Params.ALI_NOTESIGN);
        //代码模块代号
        request.putQueryParameter("TemplateCode", templateId);
        //验证码
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            i = -1;
            e.printStackTrace();
            logger.info("SMSUtils.ServerException阿里短信通道错误");
        } catch (ClientException e) {
            i = -1;
            e.printStackTrace();
            logger.info("SMSUtils.ClientException阿里短信通道错误");
        }

        return i;
    }

}

你可能感兴趣的:(阿里最新版短信工具类)