Java實(shí)現(xiàn)阿里云短信接口的示例
阿里云短信服務(wù)接口
阿里云短信服務(wù)(Short Message Service)是阿里云為用戶提供的一種通信服務(wù)的能力。
支持向國內(nèi)和國際快速發(fā)送驗(yàn)證碼、短信通知和推廣短信,服務(wù)范圍覆蓋全球200多個(gè)國家和地區(qū)。國內(nèi)短信支持三網(wǎng)合一專屬通道,與工信部攜號(hào)轉(zhuǎn)網(wǎng)平臺(tái)實(shí)時(shí)互聯(lián)。電信級(jí)運(yùn)維保障,實(shí)時(shí)監(jiān)控自動(dòng)切換,到達(dá)率高達(dá)99%。完美支撐雙11期間20億短信發(fā)送,6億用戶觸達(dá)。
快速開發(fā)
①開啟短信服務(wù)
1)登陸阿里云服務(wù)平臺(tái)
2)選擇控制臺(tái)
3)點(diǎn)擊左上角下拉按鈕選擇短信服務(wù)
4)開通短信服務(wù)
②實(shí)名認(rèn)證
1)如果沒有實(shí)名認(rèn)證需要跳轉(zhuǎn)實(shí)名認(rèn)證界面
2)選擇相應(yīng)的認(rèn)證
3)選擇支付寶快速的認(rèn)證
③創(chuàng)建簽名與模板
1)添加簽名
2)選擇簽名使用場景
驗(yàn)證碼:只能使用驗(yàn)證碼模板
通用:都可以使用(申請(qǐng)較為嚴(yán)格)
3)創(chuàng)建短信模板
4)根據(jù)常用模板庫申請(qǐng)相應(yīng)短信模板
根據(jù)使用簽名可以創(chuàng)建相應(yīng)模板,注意:驗(yàn)證碼簽名只能使用驗(yàn)證碼模板
④完成前期的準(zhǔn)備工作
1)獲取申請(qǐng)成功的簽名(注冊(cè)時(shí)的簽名名稱)
2)獲取申請(qǐng)成功的短信模板(模版code)
3)獲取AccessKey ID 和 AccessKey Secret
⑤代碼書寫
1)導(dǎo)入相應(yīng)坐標(biāo)
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.0.0</version> </dependency>
2)創(chuàng)建短信發(fā)送工具類
import com.aliyuncs.DefaultAcsClient;import com.aliyuncs.IAcsClient;import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;import com.aliyuncs.exceptions.ClientException;import com.aliyuncs.profile.DefaultProfile;import com.aliyuncs.profile.IClientProfile;public class SendSms { private static final String AccessKeyId = '';//你的accessKeyId private static final String AccessKeySecret = '';//你的accessKeySecret private static final String SignName = '';//使用的簽名 private static final String TemplateCode = '';//發(fā)送短信使用的模板 private static IAcsClient acs = null;//服務(wù)對(duì)象 private static SendSmsRequest req = new SendSmsRequest();//短信發(fā)送請(qǐng)求對(duì)象 static { IClientProfile profile = DefaultProfile.getProfile('cn-hangzhou', AccessKeyId, AccessKeySecret); acs = new DefaultAcsClient(profile); } //隨機(jī)生成指定位數(shù)驗(yàn)證碼 public static StringBuffer randomCode(int number){ //驗(yàn)證碼內(nèi)容集 final char[] CHARS = {’0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’}; StringBuffer stringBuffer=new StringBuffer(); Random r=new Random(); for(int i=0;i<number;i++){ stringBuffer.append(CHARS[r.nextInt(CHARS.length)]); } return stringBuffer; } //自定義發(fā)送方法 public static boolean sendCode(String mobile, String code) throws ClientException { req.setPhoneNumbers(mobile);//設(shè)置接收短信手機(jī)號(hào) req.setSignName(SignName);//設(shè)置使用簽名 req.setTemplateCode(TemplateCode);//設(shè)置使用通知模板id req.setTemplateParam('{'code':'' + code + ''}');//設(shè)置請(qǐng)求參數(shù) 以json字符串形式與模板一致 SendSmsResponse res = acs.getAcsResponse(req);//向服務(wù)器發(fā)送請(qǐng)求 //System.out.println('res code: ' + res.getCode());//響應(yīng)狀態(tài)碼 // System.out.println('res message: ' + res.getMessage());//響應(yīng)信息 if (res.getCode() == null && !res.getCode().equals('OK')) { System.out.println(res.getMessage()); return false; } return true; } public static void main(String[] args) throws ClientException { System.out.println(sendCode('手機(jī)號(hào)','驗(yàn)證碼')); }}
更多請(qǐng)查看阿里短信服務(wù)手冊(cè)阿里短信服務(wù)手冊(cè)
到此這篇關(guān)于Java實(shí)現(xiàn)阿里云短信接口的示例的文章就介紹到這了,更多相關(guān)Java 阿里云短信接口內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML解析錯(cuò)誤:未組織好 的解決辦法2. asp批量添加修改刪除操作示例代碼3. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析4. ASP腳本組件實(shí)現(xiàn)服務(wù)器重啟5. PHP字符串前后字符或空格刪除方法介紹6. Properties 持久的屬性集的實(shí)例詳解7. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介8. IE6/IE7/IE8/IE9中tbody的innerHTML不能賦值的完美解決方案9. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁的方法10. PHP循環(huán)與分支知識(shí)點(diǎn)梳理
