阿里云短信服务

     首先进行阿里云短信开发前最好先去看一下阿里的开发文档:https://help.aliyun.com/document_detail/55284.html?spm=5176.sms-account.0.0.164c1cbewiAW9N。阿里的开发文档还是比较给力的,简单易懂。

    我是将整个方法写进工具类里面的,方便后期调用。下面是开发代码

public class SmsUtil {

    //日志
    private static final Logger logger = LoggerFactory.getLogger(SmsUtil.class);

    //初始化ascClient需要的几个参数
    //短信API产品名称(短信产品名固定,无需修改)
    final static String product = "Dysmsapi";
    //短信API产品域名(接口地址固定,无需修改)
    final static String domain = "domain";
    //替换成你的AK
    //regionId
    final static String regoinId = "cn-hangzhou";
    //endpointName
    final static String endpointName = "cn-hangzhou";
    //accessKeyId
    final static String accessKeyId = "accessKeyId";
    //accessKeySecret
    final static String accessKeySecret = "accessKeySecret";
    //signName
    final static String signName = "signName";
    //templateCode
    final static String templateCode = "templateCode";

    public static Boolean getCode(String phone){
        //验证码
        String code = String.valueOf(Math.floor(Math.random()*900000+100000)).substring(0,6);
        //TemplateParam
        String templateParam = "{\"code\":\""+ code +"\"}";
        try {
            //初始化ascClient,暂时不支持多region(请勿修改)
            IClientProfile profile = DefaultProfile.getProfile(regoinId, accessKeyId,accessKeySecret);
            DefaultProfile.addEndpoint(endpointName,regoinId, product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            //必填:待发送手机号。
            request.setPhoneNumbers(phone);
            //必填:短信签名-可在短信控制台中找到
            request.setSignName(signName);
            //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
            request.setTemplateCode(templateCode);
            request.setTemplateParam(templateParam);
            System.out.println(templateParam);
            //请求失败这里会抛ClientException异常
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if (sendSmsResponse.getBizId() != null && sendSmsResponse.getCode().equals("OK")){
                Integer seconds = 5*60;
                //将验证码信息存入缓存,看个人需求
                RedisUtil.set("code:" + phone,code,seconds);
//                logger.info("短信内容:"+sendSmsResponse.getCode()+" "+sendSmsResponse.getMessage()+" "
//                        + sendSmsResponse.getRequestId());
                return true;
            } else {

//                userPhoneCodeVM.setFlag(false);
//                userPhoneCodeVM.setDesc("发送验证码失败");
                logger.error("短信发送失败原因: "+sendSmsResponse.getCode()+" "+sendSmsResponse.getMessage());
                return false;
            }
        } catch (Exception e) {
            logger.error("短信发送失败",e);
            return false;
        }

    }
}

    这样就能实现阿里云短信服务的调用了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值