阿里云的短信服务acsClient+java
生活随笔
收集整理的這篇文章主要介紹了
阿里云的短信服务acsClient+java
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 開通aliyun上的短信服務,主要得到
(1)訪問秘鑰 accessKeyId <==>AccessKeySecret
(2)短信簽名+模板code
參考官方文檔
2. API測試短信發送:
參考官方文檔
(1)下載sdk (原版)
(2) 測試
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; /* pom.xml <dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.16</version> </dependency> */public void sendSms( ) {// 1. 初始化acsClientDefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); IAcsClient client = new DefaultAcsClient(profile);// 2. 組裝請求對象/***http(s)://dysmsapi.aliyuncs.com/?Action=SendSms&PhoneNumbers=1381111*****&SignName=阿里云&TemplateCode=SMS_1530****&<公共請求參數>***/CommonRequest request = new CommonRequest();request.setSysMethod(MethodType.POST);request.setSysDomain("dysmsapi.aliyuncs.com"); // urlrequest.setSysVersion("2017-05-25");request.setSysAction("SendSms");/*****/request.putQueryParameter("RegionId", "cn-hangzhou");request.putQueryParameter("PhoneNumbers", phone); // 待發送手機號request.putQueryParameter("SignName", "aliyun"); // 短信簽名request.putQueryParameter("TemplateCode", "SMS_200191424"); // 短信模板code/**填充短信模板**/Map<String, String> params = new HashMap<>();params.put("station", "station");params.put("channel", "channel");params.put("alarm", "alarm");params.put("message", "message");request.putQueryParameter("TemplateParam", JSON.toJSONString(params)); // 短信模板變量對應的實際值// 3. 發送請求try {CommonResponse response = client.getCommonResponse(request); log.info(response.getData());} catch (Exception e) {log.error("短信發送失敗:", e);}}總結
以上是生活随笔為你收集整理的阿里云的短信服务acsClient+java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联发科技嵌入式_从机械到嵌入式开发,一个
- 下一篇: 关于职业发展