短信服务注册资格后申请一个测试,模版选用阿里云专用模板即可(免审核)。

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>dysmsapi20170525</artifactId>
  <version>3.1.1</version>
</dependency>

记得获取阿里云的 Access Key,它是接入凭证,放在application.yml 即可:

############ AliCloud SMS 配置 ############
aliyun:  
  accessKeyId: LTAI5tGQNtUFU8mT8pnr6avb  
  accessKeySecret: CsjjeEgG8bTeoJczWDxbl66OzcoAZm

接下来创建一些相关类,放在 sms 文件夹下:

  • AliyunAccessKeyProperties 用来接受配置文件中填写的 AccessKey 信息
@ConfigurationProperties(prefix = "aliyun")
@Component
@Data
public class AliyunAccessKeyProperties {
    private String accessKeyId;
    private String accessKeySecret;
}
  • AliyunSmsClientConfig 配置类,用于初始化一个短信发送客户端,注入到 Spring 容器中
  
import com.aliyun.dysmsapi20170525.Client;  
import com.aliyun.teaopenapi.models.Config;  
 
/**  
 * @author Edwin  
 * @date 2/22/2025 11:25 PM  
 * @description: 短信发送客户端  
 */  
@Configuration  
@Slf4j  
public class AliyunSmsClientConfig {  
  
    @Resource  
    private AliyunAccessKeyProperties aliyunAccessKeyProperties;  
  
    @Bean  
    public Client smsClient() {  
        try {  
            Config config = new Config()  
                    // 必填  
                    .setAccessKeyId(aliyunAccessKeyProperties.getAccessKeyId())  
                    // 必填  
                    .setAccessKeySecret(aliyunAccessKeyProperties.getAccessKeySecret());  
  
            // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi            config.endpoint = "dysmsapi.aliyuncs.com";  
  
            return new Client(config);  
        } catch (Exception e) {  
            log.error("初始化阿里云短信发送客户端错误: ", e);  
            return null;  
        }  
    }  
}
  • AliyunSmsHelper 短信发送工具类
import com.aliyun.dysmsapi20170525.Client;  
  
/**  
 * @author Edwin  
 * @date 2/22/2025 11:28 PM  
 * @description: TODO  
 */  
  
@Component  
@Slf4j  
public class AliyunSmsHelper {  
  
    @Resource  
    private Client client;  
  
    /**  
     * 发送短信  
     * @param signName  
     * @param templateCode  
     * @param phone  
     * @param templateParam  
     * @return  
     */  
    public boolean sendMessage(String signName, String templateCode, String phone, String templateParam) {  
        SendSmsRequest sendSmsRequest = new SendSmsRequest()  
                .setSignName(signName)  
                .setTemplateCode(templateCode)  
                .setPhoneNumbers(phone)  
                .setTemplateParam(templateParam);  
        RuntimeOptions runtime = new RuntimeOptions();  
  
        try {  
            log.info("==> 开始短信发送, phone: {}, signName: {}, templateCode: {}, templateParam: {}", phone, signName, templateCode, templateParam);  
  
            // 发送短信  
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);  
  
            log.info("==> 短信发送成功, response: {}", JsonUtil.toJsonString(response));   // JsonUtil 为自定义的
            return true;  
        } catch (Exception error) {  
            log.error("==> 短信发送错误: ", error);  
            return false;  
        }  
    }  
}

之后再使用线程池调用即可:

// 调用第三方短信发送服务
threadPoolTaskExecutor.submit(() -> {
	String signName = "阿里云短信测试";
	String templateCode = "SMS_154950909";
	String templateParam = String.format("{\"code\":\"%s\"}", verificationCode);
	aliyunSmsHelper.sendMessage(signName, templateCode, phone, templateParam);
});