目录:
- 使用阿里云开通短信服务教程
- 编写代码
使用阿里云开通短信服务教程
阿里云官网👉https://cn.aliyun.com/
完成登录
进入短信服务
开通短信服务
购买短信条数
购买完成进入阿里云短信控制台https://dysms.console.aliyun.com/overview
开通个人资质
添加签名
申请模版
在此处绑定手机号并且测试调用api发送短信
点击调用api发送短信 点击调用API发送短信,可以看到发送短信的JAVA代码。
申请阿里云秘钥,该秘钥在发送短信时会作为参数传入
点击Accesskey管理
点击创建Accesskey
编写代码
创建springboot项目
pox.xml
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>com.itbaizhan</groupId><artifactId>shopping_common</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- 阿里短信平台 --><dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>2.0.9</version></dependency><!-- dubbo --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><!-- 操作zookeeper --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>4.2.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>
application.yml
#端口号
server:port: 9007#日志格式
logging:pattern:console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
message:accessKeyId: 自己的阿里keyaccessKeySecret:自己的阿里key
MessageServiceImpl.java
package com.jjy.messagedemo.util;import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;import com.aliyun.teaopenapi.models.Config;import com.aliyun.teautil.models.RuntimeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;@Service
public class MessageServiceImpl {@Value("${message.accessKeyId}")private String accessKeyId;@Value("${message.accessKeySecret}")private String accessKeySecret;/*** 使用AK&SK初始化账号Client* @param accessKeyId* @param accessKeySecret* @return Client* @throws Exception*/private Client createClient(String accessKeyId, String accessKeySecret) throws Exception {Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);// 访问的域名config.endpoint = "dysmsapi.aliyuncs.com";return new Client(config);}public void sendMessage(String phoneNumber,String code) throws Exception {Client client = createClient(accessKeyId, accessKeySecret);SendSmsRequest sendSmsRequest = new SendSmsRequest().setSignName("智境漫游").setTemplateCode("SMS_465442065").setPhoneNumbers(phoneNumber).setTemplateParam("{\"code\":\""+code+"\"}");RuntimeOptions runtime = new RuntimeOptions();// 复制代码运行请自行打印API的返回值SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);SendSmsResponseBody body = sendSmsResponse.getBody();if ("OK".equals(body.getCode())){System.out.println("success");}else{System.out.println("error");}}
}
test.java
package com.jjy.messagedemo;import com.jjy.messagedemo.util.MessageServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class MessagedemoApplicationTests {@Autowiredprivate MessageServiceImpl messageService;@Testvoid contextLoads() throws Exception {messageService.sendMessage("15035899338","1438438");}}
注意:
因为申请的是阿里云短信个人服务,而不是企业服务,需要测试的发送手机号都必须在短信控制台进行绑定才能进行发送成功
如果我的内容对你有帮助,请点赞,评论,收藏。创作不易,大家的支持就是我坚持下去的动力