建設(shè)網(wǎng)站免費(fèi)模板百度問答庫
目錄
- 🍋發(fā)送驗證碼
- 🧣短信服務(wù)(推薦)
- 🐳注冊購買
- 🏓代碼測試
- 🖐Java組件封裝
- 🥝發(fā)送實例
- 🥝pom依賴
- 🌈Spring.factories(略)
- 🍎麻煩給博主點個關(guān)注+收藏+點贊!
🍋發(fā)送驗證碼
短信發(fā)送是電信運(yùn)營商提供的服務(wù),需要訪問對應(yīng)的接口,不同運(yùn)營商提供的接口地址肯定不一樣,如果直接訪問這些接口就需要判斷收信息的手機(jī)號屬于哪個運(yùn)營商,關(guān)鍵在于這些接口不對個人開放,還要考慮調(diào)用短信服務(wù)的費(fèi)用問題
因此目前調(diào)用短信業(yè)務(wù)都是使用第三方企業(yè)的短信服務(wù),他們與運(yùn)營商合作,封裝了短信接口,調(diào)用方法,而且費(fèi)用相對便宜
第三方的短信服務(wù)有很多,其中阿里云也提供了短信服務(wù)
🧣短信服務(wù)(推薦)
🐳注冊購買
第一步:阿里云首頁搜索短信服務(wù)
地址:添加鏈接描述
第二步:選擇購買的短信服務(wù)
第三步:點擊購買,有5條免費(fèi)使用,測試也會消耗使用次數(shù),用完了在付費(fèi)購買即可
第四步:找到自己購買的云服務(wù)
- 可以看到已購買的服務(wù)剩余數(shù)量
🏓代碼測試
第一步:參考API,在【API接口】中已經(jīng)給出了Java代碼怎么調(diào)用該服務(wù)的接口
第二步:參考API,編寫發(fā)送短信工具類
import com.aliyun.tea.TeaModel;/**** @Title:* @ClassName: com.hssmart.common.utils.AliyunSms.java* @Description:** @Copyright suihao- Powered By 研發(fā)中心* @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管理
第二部進(jìn)行查看所需要的accessKeyId 以及accessKeySecret
🖐Java組件封裝
🥝發(fā)送實例
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("測試手機(jī)號").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>