1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | package com.platform.common.utils; import com.alibaba.fastjson.JSONObject; import com.aliyun.dysmsapi20170525.Client; import com.aliyun.dysmsapi20170525.models.SendSmsRequest; import com.aliyun.dysmsapi20170525.models.SendSmsResponse; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.models.RuntimeOptions; import com.github.qcloudsms.SmsMultiSender; import com.github.qcloudsms.SmsMultiSenderResult; import com.github.qcloudsms.SmsSingleSender; import com.github.qcloudsms.SmsSingleSenderResult; import java.util.Map; /** * https://next.api.aliyun.com/api/Dysmsapi/2017-05-25/SendSms?params={}&lang=JAVA * * @author lipengjun */ public class SmsUtil { /** * 使用AK&SK初始化阿里云SMS账号Client * * @param accessKeyId accessKeyId * @param accessKeySecret accessKeySecret * @return Client * @throws Exception */ public static Client createAliSmsClient(String accessKeyId, String accessKeySecret) throws Exception { Config config = new Config() .setAccessKeyId(accessKeyId) .setAccessKeySecret(accessKeySecret) .setEndpoint( "dysmsapi.aliyuncs.com" ); return new Client(config); } /** * 指定模板ID单发短信 * 签名参数未提供或者为空时,会使用默认签名发送短信 * * @param accessKeyId accessKeyId * @param accessKeySecret accessKeySecret * @param phoneNumber 手机号 * @param templateCode 模板CODE * @param params 参数 * @param smsSign 签名 * @return SmsSingleSenderResult */ public static SendSmsResponse aliSendSms(String accessKeyId, String accessKeySecret, String phoneNumber, String templateCode, Map<String, Object> params, String smsSign) throws Exception { Client client = SmsUtil.createAliSmsClient(accessKeyId, accessKeySecret); SendSmsRequest sendSmsRequest = new SendSmsRequest(); RuntimeOptions runtime = new RuntimeOptions(); sendSmsRequest.setSignName(smsSign); sendSmsRequest.setPhoneNumbers(phoneNumber); sendSmsRequest.setTemplateCode(templateCode); sendSmsRequest.setTemplateParam(JSONObject.toJSONString(params)); return client.sendSmsWithOptions(sendSmsRequest, runtime); } /** * 指定模板ID群发 * 签名参数未提供或者为空时,会使用默认签名发送短信 * * @param appid appid * @param appkey appkey * @param nationCode 国家码,如 86 为中国 * @param phoneNumbers 手机号 * @param templateId 模板ID * @param params 参数 * @param sign 签名 * @return SmsMultiSenderResult */ public static SmsMultiSenderResult crSendSms( int appid, String appkey, String nationCode, String[] phoneNumbers, int templateId, String[] params, String sign) { SmsMultiSenderResult result = new SmsMultiSenderResult(); try { SmsMultiSender msender = new SmsMultiSender(appid, appkey); result = msender.sendWithParam(nationCode, phoneNumbers, templateId, params, sign, "" , "" ); } catch (Exception e) { // 网络IO错误 e.printStackTrace(); } return result; } /** * 指定模板ID单发短信 * 签名参数未提供或者为空时,会使用默认签名发送短信 * * @param appid appid * @param appkey appkey * @param nationCode 国家码,如 86 为中国 * @param phoneNumber 手机号 * @param templateId 模板ID * @param params 参数 * @param smsSign 签名 * @return SmsSingleSenderResult */ public static SmsSingleSenderResult crSendSms( int appid, String appkey, String nationCode, String phoneNumber, int templateId, String[] params, String smsSign) { SmsSingleSenderResult result = new SmsSingleSenderResult(); try { SmsSingleSender ssender = new SmsSingleSender(appid, appkey); result = ssender.sendWithParam(nationCode, phoneNumber, templateId, params, smsSign, "" , "" ); } catch (Exception e) { // 网络IO错误 e.printStackTrace(); } return result; } /** * 单发短信 * * @param appid appid * @param appkey appkey * @param type 短信类型,0 为普通短信,1 营销短信 * @param nationCode 国家码,如 86 为中国 * @param phoneNumber 不带国家码的手机号列表 * @param msg 短信内容,必须与申请的模板格式一致,否则将返回错误 * @return SmsSingleSenderResult */ public static SmsSingleSenderResult crSendSms( int appid, String appkey, int type, String nationCode, String phoneNumber, String msg) { SmsSingleSenderResult result = new SmsSingleSenderResult(); try { SmsSingleSender ssender = new SmsSingleSender(appid, appkey); result = ssender.send(type, nationCode, phoneNumber, msg, "" , "" ); } catch (Exception e) { // 网络IO错误 e.printStackTrace(); } return result; } /** * 群发短信 * * @param appid appid * @param appkey appkey * @param type 短信类型,0 为普通短信,1 营销短信 * @param nationCode 国家码,如 86 为中国 * @param phoneNumbers 不带国家码的手机号列表 * @param msg 短信内容,必须与申请的模板格式一致,否则将返回错误 * @return SmsMultiSenderResult */ public static SmsMultiSenderResult crSendSms( int appid, String appkey, int type, String nationCode, String[] phoneNumbers, String msg) { SmsMultiSenderResult result = new SmsMultiSenderResult(); try { SmsMultiSender msender = new SmsMultiSender(appid, appkey); result = msender.send(type, nationCode, phoneNumbers, msg, "" , "" ); } catch (Exception e) { // 网络IO错误 e.printStackTrace(); } return result; } } |