Java Code Examples for com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest#setOutId()
The following examples show how to use
com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest#setOutId() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OptSmsServiceTest.java From paascloud-master with Apache License 2.0 | 6 votes |
@Test public void sendMessageProducerTest() throws InterruptedException { SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers("13718891700"); //必填:短信签名-可在短信控制台中找到 request.setSignName("快乐学习网"); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode("SMS_78725128"); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam("{\"code\":\"963852\"}"); //选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); SendSmsResponse sendSmsResponse = optSmsService.sendSms(request); logger.info("发送 生产数据 sendSmsResponse={}", sendSmsResponse); }
Example 2
Source File: SmsUtil.java From codeway_service with GNU General Public License v3.0 | 5 votes |
/** * 发送短信 * * @param mobile 手机号 * @param template_code 模板号 * @param sign_name 签名 * @param param 参数 * @return SendSmsResponse */ public SendSmsResponse sendSms(String mobile, String template_code, String sign_name, String param) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout","10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象‐具体描述见控制台‐文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(mobile); //必填:短信签名‐可在短信控制台中找到 request.setSignName(sign_name); //必填:短信模板‐可在短信控制台中找到 request.setTemplateCode(template_code); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为$ {code}"时,此处的值为("{\"name\":\"Tom\", \"code\":\"123\"}" request.setTemplateParam(param); //选填‐上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; }
Example 3
Source File: SmsServiceImpl.java From open-capacity-platform with Apache License 2.0 | 5 votes |
@Override public SendSmsResponse sendSmsMsg(Sms sms) { if (sms.getSignName() == null) { sms.setSignName(this.signName); } if (sms.getTemplateCode() == null) { sms.setTemplateCode(this.templateCode); } // 阿里云短信官网demo代码 SendSmsRequest request = new SendSmsRequest(); request.setMethod(MethodType.POST); request.setPhoneNumbers(sms.getPhone()); request.setSignName(sms.getSignName()); request.setTemplateCode(sms.getTemplateCode()); request.setTemplateParam(sms.getParams()); request.setOutId(sms.getId().toString()); SendSmsResponse response = null; // 测试时不需要开此 add by someday begin // try { // response = acsClient.getAcsResponse(request); // if (response != null) { // log.info("发送短信结果:code:{},message:{},requestId:{},bizId:{}", response.getCode(), response.getMessage(), // response.getRequestId(), response.getBizId()); // // sms.setCode(response.getCode()); // sms.setMessage(response.getMessage()); // sms.setBizId(response.getBizId()); // } // } catch (ClientException e) { // e.printStackTrace(); // } // 测试时不需要开此 add by someday end update(sms); return response; }
Example 4
Source File: SmsUtils.java From leyou with Apache License 2.0 | 5 votes |
public SendSmsResponse sendSms(String phone, String code, String signName, String template) throws ClientException { // 可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); // 初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", prop.getAccessKeyId(), prop.getAccessKeySecret()); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); // 组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); request.setMethod(MethodType.POST); // 必填:待发送手机号 request.setPhoneNumbers(phone); // 必填:短信签名-可在短信控制台中找到 request.setSignName(signName); // 必填:短信模板-可在短信控制台中找到 request.setTemplateCode(template); // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam("{\"code\":\"" + code + "\"}"); // 选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("123456"); // hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); logger.info("发送短信状态:{}", sendSmsResponse.getCode()); logger.info("发送短信消息:{}", sendSmsResponse.getMessage()); return sendSmsResponse; }
Example 5
Source File: SmsServiceImpl.java From cloud-service with MIT License | 5 votes |
/** * 异步发送阿里云短信 * * @param sms * @return */ @Async @Override public SendSmsResponse sendSmsMsg(Sms sms) { if (sms.getSignName() == null) { sms.setSignName(this.signName); } if (sms.getTemplateCode() == null) { sms.setTemplateCode(this.templateCode); } // 阿里云短信官网demo代码 SendSmsRequest request = new SendSmsRequest(); request.setMethod(MethodType.POST); request.setPhoneNumbers(sms.getPhone()); request.setSignName(sms.getSignName()); request.setTemplateCode(sms.getTemplateCode()); request.setTemplateParam(sms.getParams()); request.setOutId(sms.getId().toString()); SendSmsResponse response = null; try { response = acsClient.getAcsResponse(request); if (response != null) { log.info("发送短信结果:code:{},message:{},requestId:{},bizId:{}", response.getCode(), response.getMessage(), response.getRequestId(), response.getBizId()); sms.setCode(response.getCode()); sms.setMessage(response.getMessage()); sms.setBizId(response.getBizId()); } } catch (ClientException e) { e.printStackTrace(); } update(sms); return response; }
Example 6
Source File: SmsUtil.java From mogu_blog_v2 with Apache License 2.0 | 5 votes |
public SendSmsResponse sendSms(String mobile, String template_code, String sign_name, String param) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(mobile); //必填:短信签名-可在短信控制台中找到 request.setSignName(sign_name); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(template_code); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam(param); //选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; }
Example 7
Source File: SmsUtil.java From NutzSite with Apache License 2.0 | 5 votes |
public static SendSmsResponse sendSms(String phoneNumber,String code) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(phoneNumber); //必填:短信签名-可在短信控制台中找到 request.setSignName(""); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode("SMS_151600445"); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam("{\"code\":\"" + code + "\"}"); //选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; }
Example 8
Source File: SmsAliStrategy.java From zuihou-admin-boot with Apache License 2.0 | 5 votes |
@Override protected SmsResult send(SmsDO smsDO) { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); try { //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsDO.getAppId(), smsDO.getAppSecret()); // DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN); DefaultProfile.addEndpoint("cn-hangzhou", PRODUCT, DOMAIN); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(smsDO.getPhone()); //必填:短信签名-可在短信控制台中找到 request.setSignName(smsDO.getSignName()); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(smsDO.getTemplateCode()); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam(smsDO.getTemplateParams()); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId(String.valueOf(smsDO.getTaskId())); log.info("阿里短信发送参数={}", JSONObject.toJSONString(request)); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); log.info("阿里短信发送结果={}", JSONObject.toJSONString(sendSmsResponse)); return SmsResult.build(ProviderType.ALI, sendSmsResponse.getCode(), sendSmsResponse.getBizId(), sendSmsResponse.getRequestId(), sendSmsResponse.getMessage(), 0); } catch (ClientException e) { log.warn("阿里短信发送失败:" + smsDO.getPhone(), e); return SmsResult.fail(e.getMessage()); } }
Example 9
Source File: AliSmsUtil.java From charging_pile_cloud with MIT License | 5 votes |
/** * 发送短信类 * * @return * @throws ClientException */ public static SendSmsResponse sendSms(AliParam ali) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ali.getAccessKeyId(), ali.getAccessKeySecret()); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(ali.getTel()); //必填:短信签名-可在短信控制台中找到 request.setSignName(ali.getSignName()); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(ali.getTemplateCode()); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam("{\"" + ali.getTemplateParam() + "\":\"" + ali.getCode() + "\"}"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; }
Example 10
Source File: SmsUtil.java From codeway_service with GNU General Public License v3.0 | 5 votes |
/** * 发送短信 * * @param mobile 手机号 * @param template_code 模板号 * @param sign_name 签名 * @param param 参数 * @return SendSmsResponse */ public SendSmsResponse sendSms(String mobile, String template_code, String sign_name, String param) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout","10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象‐具体描述见控制台‐文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(mobile); //必填:短信签名‐可在短信控制台中找到 request.setSignName(sign_name); //必填:短信模板‐可在短信控制台中找到 request.setTemplateCode(template_code); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为$ {code}"时,此处的值为("{\"name\":\"Tom\", \"code\":\"123\"}" request.setTemplateParam(param); //选填‐上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; }
Example 11
Source File: SmsapiService.java From FlyCms with MIT License | 5 votes |
public SendSmsResponse sendSms(String PhoneNumber,String code,String accessKeyId,String accessKeySecret,String signName,String templateCode) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(PhoneNumber); //必填:短信签名-可在短信控制台中找到 request.setSignName(signName); //request.setSignName("猎职网"); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(templateCode); //request.setTemplateCode("SMS_126565207"); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam("{\"name\":\"Tom\", \"code\":\""+code+"\"}"); //选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("用户名"); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; }
Example 12
Source File: AliyunSmsMessageNotifier.java From super-cloudops with Apache License 2.0 | 5 votes |
@Override public void send(GenericNotifyMessage msg) { try { isTrue(msg.getToObjects().size() < 1000, "Group numbers exceeds the limit (<1000)"); SendSmsRequest req = new SendSmsRequest(); req.setSysMethod(MethodType.POST); // 必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 req.setPhoneNumbers(StringUtils.join(msg.getToObjects(), ',')); // 必填:短信签名-可在短信控制台中找到 req.setSignName(config.getAliyun().getSignName()); // 必填:短信模板-可在短信控制台中找到 req.setTemplateCode(config.getAliyun().getTemplates().getProperty(msg.getTemplateKey())); // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 // 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 req.setTemplateParam(toJSONString(msg.getParameters())); // 可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) // request.setSmsUpExtendCode("90997"); // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 req.setOutId(msg.getCallbackId()); // 请求失败这里会抛ClientException异常 SendSmsResponse resp = acsClient.getAcsResponse(req); if (!isNull(resp) && "OK".equalsIgnoreCase(resp.getCode())) { if (log.isDebugEnabled()) log.debug("Successed response: {}, request: {}", toJSONString(resp), toJSONString(req)); else log.info("Successed message: {}, numbers: {}", resp.getMessage(), msg.getToObjects()); } else log.warn("Failed response: {}, request: {}", toJSONString(resp), toJSONString(req)); } catch (Exception e) { throw new NotificationException(kind(), e.getMessage(), e); } }
Example 13
Source File: SmsAliStrategy.java From zuihou-admin-cloud with Apache License 2.0 | 5 votes |
@Override protected SmsResult send(SmsDO smsDO) { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); try { //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsDO.getAppId(), smsDO.getAppSecret()); // DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN); DefaultProfile.addEndpoint("cn-hangzhou", PRODUCT, DOMAIN); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(smsDO.getPhone()); //必填:短信签名-可在短信控制台中找到 request.setSignName(smsDO.getSignName()); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(smsDO.getTemplateCode()); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam(smsDO.getTemplateParams()); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId(String.valueOf(smsDO.getTaskId())); log.info("阿里短信发送参数={}", JSONObject.toJSONString(request)); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); log.info("阿里短信发送结果={}", JSONObject.toJSONString(sendSmsResponse)); return SmsResult.build(ProviderType.ALI, sendSmsResponse.getCode(), sendSmsResponse.getBizId(), sendSmsResponse.getRequestId(), sendSmsResponse.getMessage(), 0); } catch (ClientException e) { log.warn("阿里短信发送失败:" + smsDO.getPhone(), e); return SmsResult.fail(e.getMessage()); } }
Example 14
Source File: UKTools.java From youkefu with Apache License 2.0 | 4 votes |
/** * 发送短信 * @param email * @param cc * @param subject * @param content * @throws Exception */ public static boolean sendSms(String phone,String id ,String template , SmsResult result) throws Exception{ SystemConfig config = UKTools.getSystemConfig() ; SmsResultRepository smsResultRepository = UKDataContext.getContext().getBean(SmsResultRepository.class); if(config!=null) { SystemMessage systemMessage = UKDataContext.getContext().getBean(SystemMessageRepository.class).findByIdAndOrgi(!StringUtils.isBlank(config.getSmsid()) ? config.getSmsid() : id,config.getOrgi()) ; if(systemMessage==null) { return false; } if(result == null) { result = new SmsResult() ; } SysDic sysDic= UKeFuDic.getInstance().getDicItem(systemMessage.getSmstype()); //阿里大于 result.setSmstext(template); if(sysDic!=null && "dysms".equals(sysDic.getCode())) { //设置超时时间-可自行调整 result.setSmstype(sysDic.getCode()); System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化ascClient需要的几个参数 final String product ="Dysmsapi";//短信API产品名称(短信产品名固定,无需修改) final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改) //替换成你的AK final String accessKeyId = systemMessage.getAppkey();//你的accessKeyId,参考本文档步骤2 final String accessKeySecret = systemMessage.getAppsec();//你的accessKeySecret,参考本文档步骤2 result.setAppkey(accessKeyId); result.setSubtime(new Date()); //初始化ascClient,暂时不支持多region(请勿修改) IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象 SendSmsRequest request = new SendSmsRequest(); //使用post提交 request.setMethod(MethodType.POST); //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 request.setPhoneNumbers(phone); //必填:短信签名-可在短信控制台中找到 request.setSignName(systemMessage.getSign()); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(systemMessage.getTpcode()); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 request.setTemplateParam(template); //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); //请求失败这里会抛ClientException异常 try { SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { result.setSendresult(sendSmsResponse.getCode()); result.setSendok(true); return true; }else if(!StringUtils.isBlank(sendSmsResponse.getMessage())){ result.setSendresult(sendSmsResponse.getMessage()); } }catch(Exception ex) { result.setSendresult(ex.getMessage()); throw ex; }finally { result.setSendtime(new Date()); smsResultRepository.save(result) ; } } } return false; }
Example 15
Source File: SmsMessageSender.java From magic-starter with GNU Lesser General Public License v3.0 | 4 votes |
/** * 业务处理 * * @param message 消息实体 * @return boolean */ @Override protected boolean process(SmsMessage message) { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", messageProperties.getSms().getAccessKey(), messageProperties.getSms().getSecretKey()); DefaultProfile.addEndpoint("cn-hangzhou", MessageConstants.SMS_PRODUCT, MessageConstants.SMS_ENDPOINT); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(message.getMobile()); //必填:短信签名-可在短信控制台中找到 request.setSignName(message.getSignName()); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(messageProperties.getSms().getChannels().get(message.getTemplateCode())); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}" request.setTemplateParam(JSONUtil.toJsonStr(message.getParams())); request.setOutId(message.getOutId()); //此处可能会抛出异常,注意catch boolean result = false; try { SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if (StrUtil.equals(MessageConstants.SMS_SUCCESS_CODE, sendSmsResponse.getCode())) { log.debug("短信发送完毕,手机号:{},返回状态:{}", message.getMobile(), sendSmsResponse.getCode()); result = true; } else { log.error("短信发送异常,手机号:{},返回状态:{}", message.getMobile(), sendSmsResponse.getCode()); } } catch (ClientException e) { log.error("短信发送异常!", e); } return result; }