基于阿里云SDK实现发送短信功能
生活随笔
收集整理的這篇文章主要介紹了
基于阿里云SDK实现发送短信功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、項目中引入maven依賴
<!-- 升級版 SDK,阿里云短信服務(wù) --> <dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>2.0.22</version> </dependency>二、在阿里云上申請內(nèi)容
1. accessKeyId
2. accessKeySecret
(上邊兩個通過點頭像-->accessKey管理 申請獲取)
3. SMS(短信模板CODE,需要申請并審批通過)
4.?signName(簽名)
(上邊三個需要在 短信服務(wù)-->國內(nèi)消息-->簽名管理、模板管理處申請)?
5.?template(短信模板變量對應(yīng)的實際值)
發(fā)送信息內(nèi)容示例:
【XXX公司】您的驗證碼是:${code},如非本人操作,請注意賬號信息安全。?
(1)簽名對應(yīng):XXX公司
(2)模板對應(yīng):您的驗證碼是:${code},如非本人操作,請注意賬號信息安全。?
(3)短信模板變量對應(yīng)的實際值:code
三、工具類
public class SendSortMsg {public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()//AccessKey ID.setAccessKeyId(accessKeyId)//AccessKey Secret.setAccessKeySecret(accessKeySecret);// 訪問的域名config.endpoint = "dysmsapi.aliyuncs.com";return new com.aliyun.dysmsapi20170525.Client(config);}public static void send(String accessKeyId, String accessKeySecret, String SMS, String tels, String signName, String template) throws Exception {com.aliyun.dysmsapi20170525.Client client = SendSortMsg.createClient(accessKeyId, accessKeySecret);com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest().setPhoneNumbers(tels) //手機號.setTemplateCode(SMS) //短信模板CODE.setTemplateParam(template) //短信模板變量對應(yīng)的實際值.setSignName(signName); //短信簽名名稱SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);System.out.println(sendSmsResponse.getBody());}總結(jié)
以上是生活随笔為你收集整理的基于阿里云SDK实现发送短信功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux系统firewalld防火墙的
- 下一篇: Latex 语法总结(持续更新)