阿里短信api接入

我将短信api写成了工具类方便页面调用

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;

/**
 *  发送短信验证码util类
 */
public class SendSmsUtil {

    public static String code;//验证码

    public static String getSendSms(String userPhone){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeyId", "secret");//accessKey签名
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", userPhone);//手机号  这里通过用户传输
        request.putQueryParameter("SignName", "短信签名");//短信签名
        request.putQueryParameter("TemplateCode", "短信模板code");//短信模板code
        request.putQueryParameter("TemplateParam", code=getSmsVerificationCode());//将生成的验证码发送给用户
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());//日志打印
        } catch (
                ServerException e) {
            e.printStackTrace();
        } catch (
                ClientException e) {
            e.printStackTrace();
        }
        return code;
    }

    //生成四位随机短信验证码
    private static String getSmsVerificationCode(){
    	//发送的验证码必须是字符串  需要拼接
        String coRed = "{\"code\":\"";

        String oneNum = String.valueOf(Math.round(Math.random()*9));
        String twoNum = String.valueOf(Math.round(Math.random()*9));
        String threeNum = String.valueOf(Math.round(Math.random()*9));
        String fourNum = String.valueOf(Math.round(Math.random()*9));

        String coEnd = "\"}";
        code = coRed + oneNum + twoNum + threeNum + fourNum + coEnd;//验证码拼接
        return code;
    }

}

以上是短信api的工具类内容,只需要在相应的地方调用即可,将签名内容这些填写成自己的就可以使用了,手机号是通过调用工具类是时候传入手机号。

接入步骤如下:

①前往阿里云官网 --> 找到 ‘短信服务’ 进入产品控制台
在这里插入图片描述

②在国内消息中 选择签名管理 --> 添加签名
在这里插入图片描述
③签名内容填写 (由于只能有一个验证码签名,我已经申请过了,所以这里我没法继续选择了)
在这里插入图片描述

④签名审核通过 --> 进入模板管理 添加模板
在这里插入图片描述
⑤这里通过之后 就可以进入快速学习 选择通过的签名和模板 发送短信验证啦!!激动人心的时候到了!!

在这里插入图片描述
全都选择了?但还是报错怎么办,别着急,解决方案有两种:①购买国内通用短信套餐包(这个比较实惠)②直接进入费用给你的账户余额充点钱,以便能正常使用阿里短信服务。
在这里插入图片描述

然后就能发送成功啦!!

下面进入实际实现阶段
选中自己的头像 进入AccessKey管理
在这里插入图片描述
选择开始使用子用户AccessKey(注意!!!! AccessKey只生成一次,请注意及时保存到记事簿或任何可以记录的东西上,AccessKey ID用于填入上面代码的accessKeyId,生成的AccessKey Secret 用于填入secret空缺)
在这里插入图片描述
然后创建一个用户
在这里插入图片描述
添加权限
在这里插入图片描述
直接添加一个管理所有阿里云资源的权限
在这里插入图片描述

现在填入信息,代码就可以运行了(调用工具类,传一个手机号

使用maven导入sdk
pom.xml

		<!--阿里短信sdk-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.3</version>
        </dependency>
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;

/**
 *  发送短信验证码util类
 */
public class SendSmsUtil {

    public static String code;//验证码

    public static String getSendSms(String userPhone){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeyId", "secret");//accessKey签名
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", userPhone);//手机号  这里通过用户传输
        request.putQueryParameter("SignName", "短信签名");//短信签名
        request.putQueryParameter("TemplateCode", "短信模板code");//短信模板code
        request.putQueryParameter("TemplateParam", code=getSmsVerificationCode());//将生成的验证码发送给用户
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());//日志打印
        } catch (
                ServerException e) {
            e.printStackTrace();
        } catch (
                ClientException e) {
            e.printStackTrace();
        }
        return code;
    }

    //生成四位随机短信验证码
    private static String getSmsVerificationCode(){
    	//发送的验证码必须是字符串  需要拼接
        String coRed = "{\"code\":\"";

        String oneNum = String.valueOf(Math.round(Math.random()*9));
        String twoNum = String.valueOf(Math.round(Math.random()*9));
        String threeNum = String.valueOf(Math.round(Math.random()*9));
        String fourNum = String.valueOf(Math.round(Math.random()*9));

        String coEnd = "\"}";
        code = coRed + oneNum + twoNum + threeNum + fourNum + coEnd;//验证码拼接
        return code;
    }

}

完结撒花

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java调用阿里云短信接口的步骤: 1. 首先,你需要在阿里云官网注册并创建一个短信服务。你可以参考阿里云官网提供的文档来了解如何创建短信服务。 2. 在你的Java项目中,你需要引入阿里云短信SDK的依赖。你可以在Maven项目中的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.aliyun</groupId> <artifactId>dysmsapi20170525</artifactId> <version>2.0.6</version> </dependency> ``` 3. 在你的Java代码中,你需要导入阿里云短信SDK的相关类和方法。例如: ```java import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.sms.model.v20170525.*; ``` 4. 创建一个DefaultAcsClient对象,并设置好AccessKey和SecretKey。例如: ```java String accessKeyId = "your_access_key_id"; String accessKeySecret = "your_access_key_secret"; IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); ``` 5. 构造一个SendSmsRequest对象,并设置好短信相关的参数,例如短信签名、模板ID、手机号码和模板参数。例如: ```java SendSmsRequest request = new SendSmsRequest(); request.setSignName("your_sign_name"); request.setTemplateCode("your_template_code"); request.setPhoneNumbers("your_phone_number"); request.setTemplateParam("{\"code\":\"123456\"}"); ``` 6. 调用client的sendSms方法发送短信,并处理发送结果。例如: ```java try { SendSmsResponse response = client.getAcsResponse(request); if (response.getCode() != null && response.getCode().equals("OK")) { System.out.println("短信发送成功"); } else { System.out.println("短信发送失败,错误码:" + response.getCode()); } } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } ``` 请注意,以上代码仅为示例,你需要根据你的实际情况进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值