先简单说下调用阿里云短信服务的基本步骤:
阿里云短信服务的官方文档:https://help.aliyun.com/product/44282.html?spm=a2c4g.11186623.6.540.18271c6eDGl8Fa
- 开通阿里云账号,申请一个子权限账户,并授予这个账户短信服务管理的权限;
- 在短信服务控制台申请签名和模板,具体规则参见审核标准:https://help.aliyun.com/document_detail/55324.html?spm=5176.12212999.0.0.51ba1cbeSAbHDQ
- 根据短信接收对象,选择相对应的签名和模板,调用接口发送
话不多说,直接上代码,该工具类集成了阿里云短信服务功能中的发送短信和查询短信发送状态的方法
package com.jeecms.common.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import java.util.*;
/**
* @date 2019年5月12日 下午2:39:02
* @TODO 使用阿里云短信服务,发送短信相关功能的工具类
*/
public class AliSmsUtils {
// private static final Log log = Log.getLog(AliSmsUtils.class);
//产品域名,开发者无需替换,以下常量基本不变
private static final String DOMAIN = "dysmsapi.aliyuncs.com";
private static final String VERSION = "2017-05-25";
private static final String SMS_ACTION = "SendSms";
private static final String QUERYSENDDETAILS_ACTION = "QuerySendDetails";
private static final String REGION_ID = "cn-hangzhou";
public static final String ACCESS_KEY_ID = "******";
public static final String ACCESS_SECRET = "***************";
public static final String DEFAULT_SIGNNAME = "**********";
public static final String DEFAULT_TEACHER_TEMPLATE = "************";
public static final String DEFAULT_MEMBER_TEMPLATE = "**********";
/**
* 用于发送短信的接口,将手机号码进行了一些处理,用list封装,可以同时进行批量发送
* @param accessKeyId 调用短信服务的秘钥,必传
* @param accessSecret 调用短信服务的密码,必传
* @param phoneNos 需要接收短信的手机号码,用list集合封装,必传
* @param SignName 发送短信时选用的签名,必传
* @param TemplateCode 发送短信时选用的模板,必传
* @param TemplateParam 短信模板变量对应的实际值,选传
* @return json格式的字符串
*/
public static String SendSmsString(String accessKeyId, String accessSecret,List<String> phoneNos,
String SignName, String TemplateCode,Map<String, String> TemplateParam) {
DefaultProfile profile = DefaultProfile.getProfile(REGION_ID,accessKeyId, accessSecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = getCommonRequest();
request.setAction(SMS_ACTION);
StringBuilder phone = new StringBuilder();
for (String phoneNo : phoneNos) {
phone.append(phoneNo).append(",");
}
String phoneNumbers = phone.substring(0, phone.lastIndexOf(","));
request.putQueryParameter("PhoneNumbers", phoneNumbers);
request.putQueryParameter("SignName", SignName);
request.putQueryParameter("TemplateCode", TemplateCode);
String jsonString = JSONObject.toJSONString(TemplateParam);
// System.out.println("jsonString的值:" + jsonString);
request.putQueryParameter("TemplateParam", jsonString);
return returnData(client, request);
}
/**
* 用于发送短信的接口
* @param accessKeyId 调用短信服务的秘钥,必传
* @param accessSecret 调用短信服务的密码,必传
* @param phoneNos 需要接收短信的手机号码,用list集合封装,必传
* @param SignName 发送短信时选用的签名,必传
* @param TemplateCode 发送短信时选用的模板,必传
* @param TemplateParam 短信模板变量对应的实际值,选传
* @return 将json格式的字符串转换为map格式后的数据
*/
public static Map<String, String> SendSmsMap(String accessKeyId, String accessSecret,List<String> phoneNos,
String SignName, String TemplateCode,Map<String, String> TemplateParam) {
String data = SendSmsString(accessKeyId, accessSecret, phoneNos,
SignName, TemplateCode, TemplateParam);
Map<String, String> map = JSONObject.parseObject(data, Map.class);
return map;
}
/**
* 用于查询短信发送状态的接口
* @param accessKeyId 调用短信服务的秘钥,必传
* @param accessSecret 调用短信服务的密码,必传
* @param phoneNumber 想要查询的手机号,必传
* @param sendDate 短信发送日期,支持查询最近30天的记录,必传
* @param pageSize 分页查看发送记录,指定每页显示的短信记录数量。取值范围为1~50。必传
* @param currentPage 分页查看发送记录,指定发送记录的的当前页码。必传
* @return json格式的字符串
*/
public static String QuerySendDetailsString(String accessKeyId,String accessSecret,
String phoneNumber, Date sendDate,Integer pageSize, Integer currentPage) {
DefaultProfile profile = DefaultProfile.getProfile(REGION_ID,accessKeyId, accessSecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = getCommonRequest();
request.setAction(QUERYSENDDETAILS_ACTION);
request.putQueryParameter("PhoneNumber", phoneNumber);
String SendDate = getDateString(sendDate);
request.putQueryParameter("SendDate", SendDate);
request.putQueryParameter("PageSize", "" + pageSize);
request.putQueryParameter("CurrentPage", "" + currentPage);
return returnData(client, request);
}
/**
* 用于查询短信发送状态的接口
* @param accessKeyId 调用短信服务的秘钥,必传
* @param accessSecret 调用短信服务的密码,必传
* @param phoneNumber 想要查询的手机号,必传
* @param sendDate 短信发送日期,支持查询最近30天的记录,必传
* @param pageSize 分页查看发送记录,指定每页显示的短信记录数量。取值范围为1~50。必传
* @param currentPage 分页查看发送记录,指定发送记录的的当前页码。必传
* @return SmsSendDetail中的详细信息,以JSONArray的格式返回
*/
public static JSONArray QuerySendDetailsJSONArray(String accessKeyId,String accessSecret,
String phoneNumber, Date sendDate,Integer pageSize, Integer currentPage) {
String data = QuerySendDetailsString(accessKeyId, accessSecret,
phoneNumber, sendDate, pageSize, currentPage);
JSONObject jsonObject = JSONObject.parseObject(data);
JSONObject smsSendDetailDTOs = (JSONObject) jsonObject.get("SmsSendDetailDTOs");
Object object = smsSendDetailDTOs.get("SmsSendDetailDTO");
JSONArray smsArray = null;
if (object instanceof JSONArray) {
smsArray = (JSONArray) object;
}
return smsArray;
}
private static CommonRequest getCommonRequest(){
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain(DOMAIN);
request.setVersion(VERSION);
request.putQueryParameter("RegionId", REGION_ID);
return request;
}
private static String returnData(IAcsClient client,CommonRequest request){
try {
CommonResponse response = client.getCommonResponse(request);
return response.getData();
} catch (ServerException e) {
e.printStackTrace();
// log.error("调用阿里云短信服务出现异常:" + e.getMessage());
} catch (ClientException e) {
e.printStackTrace();
// log.error("调用阿里云短信服务出现异常:" + e.getMessage());
}
return null;
}
private static String getDateString(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH) + 1;
return month < 10 ? "" + calendar.get(Calendar.YEAR) + 0 + month
+ calendar.get(Calendar.DATE) :
""+ calendar.get(Calendar.YEAR) + month
+ calendar.get(Calendar.DATE);
}
}
注:该工具类只集成了短信发送,和短信查询功能,如果需要以下功能,可参考其API
1.发送成功回调通知:有两种处理方式,具体参见:短信服务控制台→系统设置→通用设置
2.同时向多个不同的手机号码发送不同签名的短信: 参见https://help.aliyun.com/document_detail/102364.html?spm=a2c4g.11174283.6.615.716d2c42prHCEu