SpringBoot实现发送短信

本文介绍了如何在SpringBoot项目中实现短信发送。首先在UCPAAS平台注册并认证为个人开发者,创建项目获取短信配置。接着创建短信模板,动态参数规则为{1}{2}{3}...{n}。最后,利用Spring Boot 2.1.3和FastJson,通过RestTemplate调用短信接口,展示具体实现过程。文章适合初学者,期待社区反馈。
摘要由CSDN通过智能技术生成

一.说明

在https://office.ucpaas.com/ 注册账号并且认证为个人开发者(需要身份证),这个不难,不多说了

短信的三方平台有许多,对于选择什么平台要根据个人业务场景选择,这里只是DEMO

二.创建平台项目

创建平台项目后可以获得短信基础配置,在调用短信接口时使用

三.创建短信模板

模板动态参数设置规则为{1}{2}{3}...{n} (注意:在调用时参数之间拼接用逗号作为间隔符,这个见代码描述)

四.使用RestTemplate调用短信接口

Spring Boot 版本:2.1.3

项目中使用了阿里的开源框架FastJson,用于JSON格式字符串与JSON对象及javaBean之间的转换 ,Maven依赖如下

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.45</version>
        </dependency>
</dependencies>
复制代码

建立RestTemplate配置类,将RestTemplate注入容器中

/**
 * RestTemplate配置类
 * @Author Sans
 * @CreateTime 2019/4/2 09:55
 */
@Configuration
  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里提供一个简单的示例代码,使用Spring Boot的定时任务功能和阿里云短信服务SDK实现定时发送短信: 1. 添加阿里云短信服务SDK依赖 ```xml <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.0.0</version> </dependency> ``` 2. 在application.yml中添加阿里云短信服务配置 ```yaml aliyun: sms: accessKeyId: <your_access_key_id> accessKeySecret: <your_access_key_secret> signName: <your_sms_sign_name> templateCode: <your_sms_template_code> ``` 3. 编写定时任务类 ```java import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.sms.model.v20170525.SendSmsRequest; import com.aliyuncs.sms.model.v20170525.SendSmsResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class SmsTask { @Autowired private SmsService smsService; @Value("${aliyun.sms.signName}") private String signName; @Value("${aliyun.sms.templateCode}") private String templateCode; @Scheduled(cron = "0 0 8 * * ?") // 每天早上8点执行 public void sendSms() { String phoneNumbers = "<your_phone_number>"; String templateParam = "{\"code\":\"123456\"}"; // 短信模板参数,这里假设模板中只有一个占位符code try { SendSmsResponse response = smsService.sendSms(phoneNumbers, templateCode, templateParam, signName); if ("OK".equals(response.getCode())) { System.out.println("短信发送成功!"); } else { System.out.println("短信发送失败:" + response.getMessage()); } } catch (ClientException e) { e.printStackTrace(); } } } ``` 4. 编写短信服务类 ```java import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.sms.model.v20170525.SendSmsRequest; import com.aliyuncs.sms.model.v20170525.SendSmsResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class SmsService { @Autowired private AliyunSmsProperties aliyunSmsProperties; public SendSmsResponse sendSms(String phoneNumbers, String templateCode, String templateParam, String signName) throws ClientException { IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunSmsProperties.getAccessKeyId(), aliyunSmsProperties.getAccessKeySecret()); DefaultAcsClient client = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); request.setMethod(MethodType.POST); request.setPhoneNumbers(phoneNumbers); request.setSignName(signName); request.setTemplateCode(templateCode); request.setTemplateParam(templateParam); return client.getAcsResponse(request); } } ``` 注意事项: - 需要替换代码中的阿里云短信服务配置和手机号码 - 短信模板中的占位符和templateParam参数需要根据实际情况进行修改 - 在使用定时任务时需要开启@EnableScheduling注解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值