前提
1、开通开通阿里云短信服务
2、申请签名、申请模版
3、提前创建好阿里云接口访问的AccessKey ID
与AccessKey Secret
核心依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.2.1</version>
</dependency>
工具类
import cn.hutool.core.date.DateUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 阿里云短信发送工具类
*
* @author JHL
* @version 1.0
*/
public class SmsUtil {
private static final Logger logger = LoggerFactory.getLogger(SmsUtil.class);
/**
* 产品名称:云通信短信API产品,开发者无需替换
*/
private static final String PRODUCT = "Dysmsapi";
/**
* 产品域名,开发者无需替换
*/
private static final String DOMAIN = "dysmsapi.aliyuncs.com";
/**
* 此处需要替换成开发者自己的 AccessKey ID
*/
private static final String ACCESS_KEY_ID = "LTAI5tQDdsdsadsaCWHmwei";
/**
* 此处需要替换成开发者自己的 AccessKey Secret
*/
private static final String ACCESS_KEY_SECRET = "3DCWrodsadZdsadpL6tYzbltlD";
/**
* 发送短信
*
* @param phone 短信接收手机号
* @param smsTemplateCode (需要提前在阿里云创建) 模版代码枚举
* @param jsonTemplateVariable (需要提前在阿里云创建) json(k:v键值对)格式的模板变量,例如:{\"code\":\"1234\"}
* @return SendSmsResponse 发送短信的结果响应
* @throws ClientException e
*/
public static SendSmsResponse sendSms(String phone, SmsTemplateCodeEnum smsTemplateCode, SmsSignatureEnum smsSignature, String jsonTemplateVariable) throws ClientException {
// 超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// 初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
IAcsClient acsClient = new DefaultAcsClient(profile);
// 组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
// 必填:手机号
request.setPhoneNumbers(phone);
// 必填:设置短信签名
request.setSignName(smsSignature.getSignatureName());
// 必填:设置短信模板
request.setTemplateCode(smsTemplateCode.getTemplateCode());
// 必填:填充模板变量
request.setTemplateParam(jsonTemplateVariable);
// 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
logger.info("######################### \t[ 发送短信\t时间:{}\t电话:{}\t签名:{}\t模板:{} ]\t #########################", DateUtil.date(), phone, smsSignature.desc, smsTemplateCode.desc);
if (!"OK".equals(sendSmsResponse.getCode())) {
logger.error("######################### \t[ 短信发送失败原因:{} ]\t #########################", sendSmsResponse.getMessage());
} else {
logger.info("######################### \t[ 短信发送成功 ]\t #########################");
}
return sendSmsResponse;
}
/**
* 短信模板枚举
*/
@AllArgsConstructor
@Getter
public enum SmsTemplateCodeEnum {
TEST("测试模板", "SMS_460660514"),
ERR_TEST("错误测试模板", "123"),
;
private final String desc;
private final String templateCode;
}
/**
* 短信签名枚举
*/
@AllArgsConstructor
@Getter
public enum SmsSignatureEnum {
TEST("测试签名", "唐鸿凌个人博客"),
ERR_TEST("错误测试签名", "123"),
;
private final String desc;
private final String signatureName;
}
/**
* 测试发送
*/
public static void main(String[] args) throws ClientException {
sendSms("1x5998x316x", SmsTemplateCodeEnum.ERR_TEST, SmsSignatureEnum.TEST, null);
}
}
本文参考:https://www.zhihu.com/column/p/414819277
标签:短信,String,接入,private,阿里,import,com,final From: https://www.cnblogs.com/hhddd-1024/p/17378227.html