阿里云实现发送短信(Java实例教程)
生活随笔
收集整理的這篇文章主要介紹了
阿里云实现发送短信(Java实例教程)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 🍋發送驗證碼
- 🧣短信服務(推薦)
- 🐳注冊購買
- 🏓代碼測試
- 🖐Java組件封裝
- 🥝發送實例
- 🥝pom依賴
- 🌈Spring.factories(略)
- 🍎麻煩給博主點個關注+收藏+點贊!
🍋發送驗證碼
短信發送是電信運營商提供的服務,需要訪問對應的接口,不同運營商提供的接口地址肯定不一樣,如果直接訪問這些接口就需要判斷收信息的手機號屬于哪個運營商,關鍵在于這些接口不對個人開放,還要考慮調用短信服務的費用問題
因此目前調用短信業務都是使用第三方企業的短信服務,他們與運營商合作,封裝了短信接口,調用方法,而且費用相對便宜
第三方的短信服務有很多,其中阿里云也提供了短信服務
🧣短信服務(推薦)
🐳注冊購買
第一步:阿里云首頁搜索短信服務
地址:添加鏈接描述
第二步:選擇購買的短信服務
第三步:點擊購買,有5條免費使用,測試也會消耗使用次數,用完了在付費購買即可
第四步:找到自己購買的云服務
- 可以看到已購買的服務剩余數量
🏓代碼測試
第一步:參考API,在【API接口】中已經給出了Java代碼怎么調用該服務的接口
第二步:參考API,編寫發送短信工具類
import com.aliyun.tea.TeaModel;/**** @Title:* @ClassName: com.hssmart.common.utils.AliyunSms.java* @Description:** @Copyright suihao- Powered By 研發中心* @author: suihao* @date: 2022-11-01 15:51* @version V1.0*/ public class AliyunSms {/*** 使用AK&SK初始化賬號Client* @param accessKeyId * @param accessKeySecret* @return Client* @throws Exception*/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);}}accessKeyId 以及accessKeySecret查找的方式“:
第一步點擊頭像打開accessKey管理
第二部進行查看所需要的accessKeyId 以及accessKeySecret
🖐Java組件封裝
🥝發送實例
package com.suihao.autoconfig.properties;public static void main(String[] args) {com.aliyun.dysmsapi20170525.Client client = AliyunSms.createClient("accessKeyId", "accessKeySecret");com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest().setSignName("簽名名稱").setTemplateCode("模板號碼").setPhoneNumbers("測試手機號").setTemplateParam("{\"code\":\"6666\"}");com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();com.aliyun.dysmsapi20170525.models.SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));}1.SignName代表的簽名名稱
2.TemplateCode代表的模板code
🥝pom依賴
<!--阿里云--><developers><developer><id>aliyundeveloper</id><name>Aliyun SDK</name><email>aliyunsdk@aliyun.com</email></developer></developers><distributionManagement><snapshotRepository><id>sonatype-nexus-snapshots</id><url>https://s01.oss.sonatype.org/content/repositories/snapshots</url></snapshotRepository><repository><id>sonatype-nexus-staging</id><url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url></repository></distributionManagement><scm><connection></connection><developerConnection></developerConnection><url></url></scm><dependencies><!--阿里云--><dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>2.0.22</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>tea-openapi</artifactId><version>0.2.6</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>tea-console</artifactId><version>0.0.1</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>tea-util</artifactId><version>0.2.14</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>tea</artifactId><version>1.1.14</version></dependency><!----></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.sonatype.plugins</groupId><artifactId>nexus-staging-maven-plugin</artifactId><version>1.6.3</version><extensions>true</extensions><configuration><serverId>sonatype-nexus-staging</serverId><nexusUrl>https://s01.oss.sonatype.org/</nexusUrl><autoReleaseAfterClose>true</autoReleaseAfterClose></configuration></plugin></plugins></build>🌈Spring.factories(略)
🍎麻煩給博主點個關注+收藏+點贊!
總結
以上是生活随笔為你收集整理的阿里云实现发送短信(Java实例教程)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智慧养老解决方案之非接触式查房系统
- 下一篇: 老李分享:《Java Performan