1.首先注册阿里云账号
2.登录成功后会出现这个界面
选中里面的短信
然后点击免费开通,开通的话需要实名制,你只要用支付宝授权下就可以了
授权成功后点击新增签名,这个可以使用发验证的也可以使用发短信的,只不过,发短信的在审核时候比较严格,那个可能还需要公司,或者可支付的公众号的一些信息,普通的比较好审核,只是那个权限也比较小只能发验证码,签名成功后,然后在去申请模板,模板那个比较好通过审核,那个模板就是发送短信的内容模板
在这些操作完成之后还不行,还得申请一个accesskey号,要用
没错就是上面我打码的那俩号
在这些准备好之后就ok了可以上代码了
打开你的idea将阿里云的这个坐标放到指定的pom文件下
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
在这解释几个参数的含义
cn-hangzhou------地区(这里可以写默认default)
<accessKeyId>-----accessKeyId就是第二次申请的那个参数看上面截图
<accessSecret>-----accessSecret就是第二次申请的那个参数看上面截图
RegionId----模板号code看上面截图
phone----指定的接收短信人的手机号
codeNum-----随机出来的验证码
1 import com.aliyuncs.CommonRequest; 2 import com.aliyuncs.CommonResponse; 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.http.MethodType; 8 import com.aliyuncs.profile.DefaultProfile; 9 /* 10 pom.xml 11 <dependency> 12 <groupId>com.aliyun</groupId> 13 <artifactId>aliyun-java-sdk-core</artifactId> 14 <version>4.0.3</version> 15 </dependency> 16 */ 17 public class SendSms { 18 public static void main(String[] args) { 19 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>"); 20 IAcsClient client = new DefaultAcsClient(profile); 21 CommonRequest request = new CommonRequest(); 22 request.setMethod(MethodType.POST); 23 request.setDomain("dysmsapi.aliyuncs.com"); 24 request.setVersion("2017-05-25"); 25 request.setAction("SendSms"); 26 request.putQueryParameter("RegionId", "cn-hangzhou"); 27 request.putQueryParameter("PhoneNumbers", phone); 28 request.putQueryParameter("TemplateParam", "{\"code\":\""+codeNum+"\"}"); 29 30 try { 31 CommonResponse response = client.getCommonResponse(request); 32 System.out.println(response.getData()); 33 } catch (ServerException e) { 34 e.printStackTrace(); 35 } catch (ClientException e) { 36 e.printStackTrace(); 37 } 38 } 39 }
成功的数据为
可以关注秦川以北大家一起学习一起进步!!!