java实现阿里云接口发送短信验证码
生活随笔
收集整理的這篇文章主要介紹了
java实现阿里云接口发送短信验证码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
版本
aliyun-java-sdk-core 4.4.6aliyun-java-sdk-ecs 4.17.6請求參數(shù)
返回數(shù)據(jù)
返回錯誤碼請看我的另一篇文章 阿里云短信錯誤碼
如何開通阿里云賬號即開啟短信服務 如何開啟阿里云短信服務
示例
package com.ncst;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;public class SendMs {public static void main(String[] args) {//隨機驗證碼int newcode = (int) (Math.random() * 9999) + 100;//手機號long num = 15028999627L;CommonResponse response = sendMs(Integer.toString(newcode), Long.toString(num));System.out.println(response.getData());}/*** 發(fā)送短信** @param code* @param num* @return*/public static CommonResponse sendMs(String code, String num) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "你的阿里云:AccessKey ID", "你的阿里云:AccessKey Secret");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");//必填:待發(fā)送手機號request.putQueryParameter("PhoneNumbers", num);//必填:短信簽名-可在短信控制臺中找到request.putQueryParameter("SignName", "你的阿里云短信簽名");//必填:短信模板-可在短信控制臺中找到request.putQueryParameter("TemplateCode", "你的阿里云短信模板");//可選:模板中的變量替換JSON串,如模板內(nèi)容為"親愛的${name},您的驗證碼為${code}"時,此處的值為request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");try {CommonResponse response = client.getCommonResponse(request);return response;} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}throw new RuntimeException("ERROR");} }運行效果
總結(jié)
以上是生活随笔為你收集整理的java实现阿里云接口发送短信验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我重仓了BCH
- 下一篇: 销量持续下跌涨价或许会让苹果业绩雪上加霜