阿里云通信短信接口实现
2019-03-10 10:25
127 查看
1、阿里云通信官方文档
点击此处查看:云通信短信文档
看过文档之后,我们首先需要引入SDK依赖包:使用maven在pom文件中添加相关依赖
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.0.6</version> <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 --> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.1.0</version> </dependency>
2、准备工作
从上图我们可以看出在写代码之前还需要一些准备工作:申请短信签名、申请短信模板。至于如何申请请看官方文档,我们直接进入主题:
3、编写Demo
private AppUserSmsMapper appUserSmsMapper; //产品名称:云通信短信API产品,开发者无需替换 private static final String product = "Dysmsapi"; //产品域名,开发者无需替换 private static final String domain = "dysmsapi.aliyuncs.com"; // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找) private static String accessKeyId = "你的AccessKeyId"; private static String accessKeySecret = "你的AccessKeySecret"; public static String signName = "你的短信签名"; public static String[] templatecode = { //你的短信模板,如果有多个短信模板可以放在数组中,比如 "SMS_XXXXXXXXX ", "SMS_XXXXXXXXX ", "SMS_XXXXXXXXX " }; public static SendSmsResponse sendSms(String phone,int type,String JsonMsg,String extend) throws ClientException { if(phone.length() == 0){ return null; } //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 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(); //使用post提交 request.setMethod(MethodType.POST); //必填:待发送手机号 request.setPhoneNumbers(phone); //必填:短信签名-可在短信控制台中找到 request.setSignName(signName); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(templatecode[type]); //可选:模板中的变量替换JSON串,如模板内容为"验证码${code},您正在注册成为新用户,感谢您的支持!"时,此处的值为 request.setTemplateParam(JsonMsg); //选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 if(extend != null){ request.setOutId("yourOutId"); } SendSmsResponse sendSmsResponse = null; //hint 此处可能会抛出异常,注意catch try{ sendSmsResponse = acsClient.getAcsResponse(request); }catch(Exception e){ return null; } return sendSmsResponse; } public static void main(String[] args) throws ClientException, InterruptedException { //发短信 Map map = new HashMap(); map.put("code", "123456");//往里面放验证码 SendSmsResponse response = sendSms("这里填写手机号",“需要模板在数组中的下标”,JSONObject.fromObject(map).toString(),null); System.out.println("短信接口返回的数据----------------"); System.out.println("Code=" + response.getCode()); System.out.println("Message=" + response.getMessage()); System.out.println("RequestId=" + response.getRequestId()); System.out.println("BizId=" + response.getBizId()); System.out.println("BizId=" + response.getBizId()); }
4、测试
如果在控制台中看到这个说明已经成功了,
这个是出现了错误,因为短信签名没有审核通过,其他错误提示,你可以复制Code百度。
相关文章推荐
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- php实现短信验证(阿里云通信)和邮箱验证(phpmailer)
- 调用阿里云接口实现短信消息的发送源码——CSDN博客
- JAVA实现阿里云接口完成短信验证
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送功能
- TP5调用阿里云短信接口实现发送短信功能
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送