阿里云-----云通信短信服务---发送验证码到手机

阿里什么都做啊,今天发现了短信服务,测试了一下,可用,简单方便,不需要太多时间便搞定了,文章最后有源码下载。


1、首先你需要有阿里云账号---然后开通短信服务,记得在阿里云账号充2元,不然发送不成功

https://www.aliyun.com/product/sms?spm=5176.doc25420.416540.56.567Vu7

阿里云-----云通信短信服务---发送验证码到手机

************************************************

阿里云-----云通信短信服务---发送验证码到手机

**************************************************************

阿里云-----云通信短信服务---发送验证码到手机

**************************价格**********************************

阿里云-----云通信短信服务---发送验证码到手机

2、首先看下你需要准备哪些参数

[java] view plain copy
  1. /**********需要准备的参数**************/  
  2.     public static String accessKey="";//需要修改  
  3.     public static String accessSecret="";//需要修改  
  4.     public static String code="SMS_41635111";//需要修改  
  5.     public static String signName="测试99";//需要修改  

创建和查看Access Key

https://ak-console.aliyun.com/#/accesskey/

阿里云-----云通信短信服务---发送验证码到手机

3、开通短信服务成功后,需要配置短信签名和短信模板,配置完后,需要审核,审核过后就拿了codesignName两个参数

阿里云-----云通信短信服务---发送验证码到手机


4、创建短信签名

阿里云-----云通信短信服务---发送验证码到手机

注意

阿里云-----云通信短信服务---发送验证码到手机

审核成功

阿里云-----云通信短信服务---发送验证码到手机


5、创建短信模板

阿里云-----云通信短信服务---发送验证码到手机

审核成功

阿里云-----云通信短信服务---发送验证码到手机


这里面有一个不成功,原因是我想用短信通知来发验证码

sms01:         ${name},哈哈,现在时间是${time}

sms02:         ${name},我用短信通知发送验证码不行啊,验证码为${code},时间${time},祝生活愉快。



6、短信签名和短信模板审核通过后就可以开发了


7、主要代码

[java] view plain copy
  1. package com.kp.sms;  
  2.   
  3. import com.aliyuncs.DefaultAcsClient;  
  4. import com.aliyuncs.IAcsClient;  
  5. import com.aliyuncs.exceptions.ClientException;  
  6. import com.aliyuncs.exceptions.ServerException;  
  7. import com.aliyuncs.profile.DefaultProfile;  
  8. import com.aliyuncs.profile.IClientProfile;  
  9. import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;  
  10. import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;  
  11.   
  12. /**  
  13.  * @author: py 
  14.  * @version:2017年1月13日 下午2:40:28  
  15.  * com.kp.sms.TestSms.java 
  16.  * @Desc  
  17.  */  
  18. public class TestSms {  
  19.     public static String regionId="cn-hangzhou";//机房信息,可以不用更改  
  20.     /**********需要准备的参数**************/  
  21.     public static String accessKey="";//需要修改  
  22.     public static String accessSecret="";//需要修改  
  23.     public static String code="SMS_41635111";//需要修改  
  24.     public static String signName="测试99";//需要修改  
  25.     /**********************************/  
  26.       
  27.       
  28.     public static void main(String[] args) {  
  29.           
  30.         String phone="15589895656";  
  31.         String time =getChinaDateByMM(System.currentTimeMillis());  
  32.         //根据自己定义的短信模板,修改  
  33.         String jsonStr="{\"name\":\"小明\",\"code\":\"12312\",\"time\":\""+time +"\"}";  
  34.         test(phone, jsonStr,code,signName);  
  35.   
  36.     }  
  37.   
  38.       
  39.        public static void test(String phone, String jsonStr, String code, String signName) {          
  40.            try {  
  41.             IClientProfile profile = DefaultProfile.getProfile(regionId, accessKey, accessSecret);  
  42.             DefaultProfile.addEndpoint("cn-hangzhou""cn-hangzhou""Sms",  "sms.aliyuncs.com");  
  43.             IAcsClient client = new DefaultAcsClient(profile);  
  44.             SingleSendSmsRequest request = new SingleSendSmsRequest();  
  45.                 //管理控制台中配置的短信签名(状态必须是验证通过)  
  46.                 request.setSignName(signName);  
  47.                 //管理控制台中配置的审核通过的短信模板的模板CODE(状态必须是验证通过)  
  48.                  request.setTemplateCode(code);  
  49. //              短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。  
  50. //               例如:短信模板为:“接受短信验证码${no}”,此参数传递{“no”:”123456”},用户将接收到[短信签名]接受短信验证码123456  
  51.                 request.setParamString(jsonStr);  
  52.                 //目标手机号,多个手机号可以逗号分隔  
  53.                 request.setRecNum(phone);  
  54. //              request.setVersion(version);  
  55.                   
  56.                 SingleSendSmsResponse httpResponse = client.getAcsResponse(request);  
  57.                 String requestId = httpResponse.getRequestId();  
  58.                 System.err.println("requestId:"+requestId);  
  59.             } catch (ServerException e) {  
  60.                 e.printStackTrace();  
  61.             }  
  62.             catch (ClientException e) {  
  63.                 e.printStackTrace();  
  64.             }  
  65.         }  
  66.          
  67.          
  68.         /** 
  69.          * 使用毫秒转换为中文日期 
  70.          * @param tmpDateInt 
  71.          * @return 
  72.          */  
  73.         public static String getChinaDateByMM(long time){  
  74.             String ret_date = "";  
  75.             java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy年MM月dd日");  
  76.             ret_date = formatter.format(time);  
  77.             return ret_date;  
  78.         }  
  79. }  

返回值

阿里云-----云通信短信服务---发送验证码到手机

8、成功

阿里云-----云通信短信服务---发送验证码到手机


9、源码:

http://download.****.net/detail/u014520797/9738807


10、注意:有些账号运行该代码会报错,原因可能如下:

阿里云-----云通信短信服务---发送验证码到手机

*************************

阿里云-----云通信短信服务---发送验证码到手机

短信服务这个产品已经整合到消息服务MNS中了,需要使用MNS的sdk来发送短信。

JAVA SDK:https://help.aliyun.com/document_detail/51063.html
Python SDK:https://help.aliyun.com/document_detail/51372.html
C# SDK:https://help.aliyun.com/document_detail/52016.html
PHP SDK: https://help.aliyun.com/document_detail/51929.html


晚点我会写一篇新文章解决大家的疑惑

http://blog.****.net/u014520797/article/details/71171880