springboot定时发送短信代码
时间: 2023-11-05 20:00:39 浏览: 98
这里提供一个简单的示例代码,使用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注解
阅读全文