package com.ocsmarter.alarm.utils;
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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
// 测试短信工具类
public class TestSMSUtil {
// 签名
private static final String signName = "数字科技";
// 模板
private static final String templateCode = "SMS_297961352";
// 阿里云短信配置信息
private static final String accessKeyId = "LTAI5tPnXjhzMyLfhSx*";
private static final String accessKeySecret = "fNmjfvbMGPiJcjx81gcY0*";
private static final String REGION_ID = "cn-hangzhou";
private static final String PRODUCT = "Dysmsapi";
private static final String DOMAIN = "dysmsapi.aliyuncs.com";
/**
* 发送短信通知
*
* @param mobile 手机号
* @param code 验证码
* @return 执行结果
*/
public static boolean sendSMS(String mobile, String code) {
try {
IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setMethod(MethodType.POST);
// 手机号可以单个也可以多个(多个用逗号隔开,如:15*******13,13*******27,17*******56)
request.setPhoneNumbers(mobile);
request.setSignName(signName);
request.setTemplateCode(templateCode);
/* 例如签名内容为:某某公司
例如模板内容为:亲爱的同事,很高兴的通知您,您抽中了由领导${userName}派发的大饼奖励${money}元,请及时找财务领取!
变量属性:userName-其他;money-其他;
则短信内容为:【某某公司】 亲爱的同事,很高兴的通知您,您抽中了由领导${userName}派发的大饼奖励${money}元,请及时找财务领取!*/
request.setTemplateParam("{\"code\":\""+ code +"\"}");
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if ((sendSmsResponse.getCode() != null) && (sendSmsResponse.getCode().equals("OK"))) {
System.out.println("发送成功,code:" + sendSmsResponse.getCode());
return true;
} else {
System.out.println("发送失败,code:" + sendSmsResponse.getCode());
return false;
}
} catch (ClientException e) {
System.out.println("发送失败,系统错误!");
return false;
}
}
public static void main(String[] args) {
System.out.println(TestSMSUtil.sendSMS("18810557753", "5678"));
}
}
05-24