簡單的調用阿里雲的短信接口


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   }

 

成功的數據為

可以關注秦川以北大家一起學習一起進步!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM