短信发送是电信运营商提供的服务,需要访问对应的接口,不同运营商提供的接口地址肯定不一样,如果直接访问这些接口就需要判断收信息的手机号属于哪个运营商,关键在于这些接口不对个人开放,还要考虑调用短信服务的费用问题
因此目前调用短信业务都是使用第三方企业的短信服务,他们与运营商合作,封装了短信接口,调用方法,而且费用相对便宜
第三方的短信服务有很多,其中阿里云也提供了短信服务
第一步:阿里云首页搜索短信服务
第三步:点击购买,有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
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
<developers>
<developer>
<id>aliyundeveloperid>
<name>Aliyun SDKname>
<email>[email protected]email>
developer>
developers>
<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshotsid>
<url>https://s01.oss.sonatype.org/content/repositories/snapshotsurl>
snapshotRepository>
<repository>
<id>sonatype-nexus-stagingid>
<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.aliyungroupId>
<artifactId>dysmsapi20170525artifactId>
<version>2.0.22version>
dependency>
<dependency>
<groupId>com.aliyungroupId>
<artifactId>tea-openapiartifactId>
<version>0.2.6version>
dependency>
<dependency>
<groupId>com.aliyungroupId>
<artifactId>tea-consoleartifactId>
<version>0.0.1version>
dependency>
<dependency>
<groupId>com.aliyungroupId>
<artifactId>tea-utilartifactId>
<version>0.2.14version>
dependency>
<dependency>
<groupId>com.aliyungroupId>
<artifactId>teaartifactId>
<version>1.1.14version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.sonatype.pluginsgroupId>
<artifactId>nexus-staging-maven-pluginartifactId>
<version>1.6.3version>
<extensions>trueextensions>
<configuration>
<serverId>sonatype-nexus-stagingserverId>
<nexusUrl>https://s01.oss.sonatype.org/nexusUrl>
<autoReleaseAfterClose>trueautoReleaseAfterClose>
configuration>
plugin>
plugins>
build>