Java Code Examples for com.aliyuncs.profile.DefaultProfile#getProfile()
The following examples show how to use
com.aliyuncs.profile.DefaultProfile#getProfile() .
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: ApiBootAliYunSmsService.java From api-boot with Apache License 2.0 | 6 votes |
/** * invoke send SMS * * @param request {@link ApiBootSmsRequest} * @return {@link ApiBootSmsResponse} * @throws ApiBootException ApiBoot Exception */ @Override public ApiBootSmsResponse send(ApiBootSmsRequest request) throws ApiBootException { try { IClientProfile profile = DefaultProfile.getProfile(this.profile, this.accessKeyId, this.accessKeySecret); DefaultProfile.addEndpoint(this.profile, ALIYUN_PRODUCT, ALIYUN_PRODUCT_DOMAIN); IAcsClient acsClient = new DefaultAcsClient(profile); SendSmsRequest sendSmsRequest = new SendSmsRequest(); sendSmsRequest.setPhoneNumbers(request.getPhone()); sendSmsRequest.setSignName(this.signName); sendSmsRequest.setTemplateCode(request.getTemplateCode()); sendSmsRequest.setTemplateParam(request.getParam().getParamJson()); SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(sendSmsRequest); return ApiBootSmsResponse.builder().success(SUCCESS_RESULT.equals(sendSmsResponse.getCode())).build(); } catch (Exception e) { throw new ApiBootException("invoke send SMS have Exception:" + e.getMessage()); } }
Example 2
Source File: SimaplePhoneRegist.java From danyuan-application with Apache License 2.0 | 5 votes |
public void SendPhoneToCustom(String phone, String templateParam) throws ClientException { // 设置超时时间-可自行调整 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 // 初始化ascClient,暂时不支持多region(请勿修改) IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", PHONE_ACCESSKEYID, PHONE_ACCESSKEYSECRET); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); // 组装请求对象 SendSmsRequest request = new SendSmsRequest(); // 使用post提交 request.setMethod(MethodType.POST); // 必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为国际区号+号码,如“85200000000” request.setPhoneNumbers(phone); // 必填:短信签名-可在短信控制台中找到 request.setSignName(PHONE_SIGNNAME); // 必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版 request.setTemplateCode(PHONE_TEMPLATECODE); // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 // 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 request.setTemplateParam(templateParam); // 可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) // request.setSmsUpExtendCode("90997"); // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 // request.setOutId("yourOutId"); // 请求失败这里会抛ClientException异常 SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { // 请求成功 } }
Example 3
Source File: AliyunSmsMessageNotifier.java From super-cloudops with Apache License 2.0 | 5 votes |
@Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); AliyunSmsNotifyProperties aliyun = config.getAliyun(); // 设置超时时间-可自行调整 System.setProperty("sun.net.client.defaultConnectTimeout", aliyun.getDefaultConnectTimeout()); System.setProperty("sun.net.client.defaultReadTimeout", aliyun.getDefaultReadTimeout()); // 初始化ascClient,暂时不支持多region(请勿修改) IClientProfile profile = DefaultProfile.getProfile(aliyun.getRegionId(), aliyun.getAccessKeyId(), aliyun.getAccessKeySecret()); acsClient = new DefaultAcsClient(profile); }
Example 4
Source File: SmsOtpAuthnAliyun.java From MaxKey with Apache License 2.0 | 5 votes |
@Override public boolean produce(UserInfo userInfo) { // 手机号 String mobile = userInfo.getMobile(); if (mobile != null && !mobile.equals("")) { try { DefaultProfile profile = DefaultProfile.getProfile( "cn-hangzhou", accessKeyId, accessSecret); IAcsClient client = new DefaultAcsClient(profile); String token = this.genToken(userInfo); CommonRequest request = new CommonRequest(); request.setSysMethod(MethodType.POST); request.setSysDomain("dysmsapi.aliyuncs.com"); request.setSysVersion("2017-05-25"); request.setSysAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("PhoneNumbers", mobile); request.putQueryParameter("SignName", signName); request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateParam", "{\"code\":\"" + token + "\"}"); CommonResponse response = client.getCommonResponse(request); logger.debug("responseString " + response.getData()); //成功返回 if (response.getData().indexOf("OK") > -1) { this.optTokenStore.store( userInfo, token, userInfo.getMobile(), OptTypes.SMS); return true; } } catch (Exception e) { logger.error(" produce code error ", e); } } return false; }
Example 5
Source File: AliyunUtil.java From roncoo-education with MIT License | 5 votes |
public static boolean sendMsg(String phone, String code, Aliyun aliyun) throws ClientException { System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); // 初始化ascClient需要的几个参数 // 初始化ascClient,暂时不支持多region(请勿修改) IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyun.getAliyunAccessKeyId(), aliyun.getAliyunAccessKeySecret()); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com"); IAcsClient acsClient = new DefaultAcsClient(profile); // 组装请求对象 SendSmsRequest request = new SendSmsRequest(); // 使用post提交 request.setMethod(MethodType.POST); // 必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 request.setPhoneNumbers(phone); // 必填:短信签名-可在短信控制台中找到 request.setSignName(aliyun.getSignName()); // 必填:短信模板-可在短信控制台中找到 request.setTemplateCode(aliyun.getSmsCode()); // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 // 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 request.setTemplateParam("{\"code\":\"{code}\",\"product\":\"领课开源\"}".replace("{code}", code)); // 可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) // request.setSmsUpExtendCode("90997"); // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 // request.setOutId("yourOutId"); // 请求失败这里会抛ClientException异常 SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { // 请求成功 log.debug(sendSmsResponse.getCode()); return true; } else { log.error("发送失败,code={},message={}", sendSmsResponse.getCode(), sendSmsResponse.getMessage()); return false; } }
Example 6
Source File: SmsUtil.java From mogu_blog_v2 with Apache License 2.0 | 5 votes |
public QuerySendDetailsResponse querySendDetails(String mobile, String bizId) 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); //组装请求对象 QuerySendDetailsRequest request = new QuerySendDetailsRequest(); //必填-号码 request.setPhoneNumber(mobile); //可选-流水号 request.setBizId(bizId); //必填-发送日期 支持30天内记录查询,格式yyyyMMdd SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd"); request.setSendDate(ft.format(new Date())); //必填-页大小 request.setPageSize(10L); //必填-当前页码从1开始计数 request.setCurrentPage(1L); //hint 此处可能会抛出异常,注意catch QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request); return querySendDetailsResponse; }
Example 7
Source File: SmsClient.java From aliyun-sms with Apache License 2.0 | 5 votes |
/** * Instantiates a new SmsClient. * * @param accessKeyId 阿里云短信 accessKeyId * @param accessKeySecret 阿里云短信 accessKeySecret * @param smsTemplates 预置短信模板 */ public SmsClient(final String accessKeyId, final String accessKeySecret, final Map<String, SmsTemplate> smsTemplates) { checkNotEmpty(accessKeyId, "'accessKeyId' must be not empty"); checkNotEmpty(accessKeySecret, "'accessKeySecret' must be not empty"); final IClientProfile clientProfile = DefaultProfile.getProfile( "default", accessKeyId, accessKeySecret); this.acsClient = new DefaultAcsClient(clientProfile); this.smsTemplates = smsTemplates; }
Example 8
Source File: SmsUtil.java From codeway_service with GNU General Public License v3.0 | 5 votes |
/** * 查询发送详情 * @param mobile * @param bizId * @return QuerySendDetailsResponse */ public QuerySendDetailsResponse querySendDetails(String mobile, String bizId) 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); //组装请求对象 QuerySendDetailsRequest request = new QuerySendDetailsRequest(); //必填‐号码 request.setPhoneNumber(mobile); //可选‐流水号 request.setBizId(bizId); //必填‐发送日期 支持30天内记录查询,格式yyyyMMdd SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd"); request.setSendDate(ft.format(new Date())); //必填‐页大小 request.setPageSize(10L); //必填‐当前页码从1开始计数 request.setCurrentPage(1L); //hint 此处可能会抛出异常,注意catch QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request); return querySendDetailsResponse; }
Example 9
Source File: SmsSenderAliyunImpl.java From spring-backend-boilerplate with Apache License 2.0 | 5 votes |
public SmsSenderAliyunImpl(AdvancedAliyunOptions options) { this.options = options; try { IClientProfile profile = DefaultProfile.getProfile(options.getArea(), options.getAccessKey(), options.getAccessSecret()); DefaultProfile.addEndpoint(options.getArea(), options.getArea(), "Sms", options.getSmsEndpoint()); client = new DefaultAcsClient(profile); } catch (Throwable e) { throw new SmsException("Initialize the aliyun sms client failed.", e); } }
Example 10
Source File: DMServiceImpl.java From jframe with Apache License 2.0 | 5 votes |
@Start void start() { LOG.info("Start DMService"); try { String file = plugin.getConfig(FILE_ALIDM, plugin.getConfig(Config.APP_CONF) + "/alidm.properties"); if (!new File(file).exists()) { throw new FileNotFoundException("not found " + file); } _config.init(file); for (String id : _config.getGroupIds()) { // 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求 IClientProfile profile = DefaultProfile.getProfile(_config.getConf(id, K_regionId), _config.getConf(id, K_accessKeyId), _config.getConf(id, K_accessKeySecret)); // https://help.aliyun.com/document_detail/96856.html?spm=a2c4g.11186623.6.588.476d3a35bcMFGs String regionId = _config.getConf(id, K_regionId, "cn-hangzhou"); String product = _config.getConf(id, K_product, "Dm"); String domain = _config.getConf(id, K_domain, "dm.aliyuncs.com"); DefaultProfile.addEndpoint(regionId, product, domain); DefaultAcsClient client = new DefaultAcsClient(profile); clients.put(id, client); } } catch (Exception e) { LOG.error("Start DMService Failure!" + e.getMessage(), e); return; } LOG.info("Start DMService Successfully!"); }
Example 11
Source File: AliyunSmsConfiguration.java From paascloud-master with Apache License 2.0 | 5 votes |
/** * Acs client acs client. * * @return the acs client * * @throws ClientException the client exception */ @Bean public IAcsClient acsClient() throws ClientException { log.info("SMS Bean IAcsClient Start"); IClientProfile profile = DefaultProfile.getProfile(paascloudProperties.getAliyun().getSms().getRegionId(), paascloudProperties.getAliyun().getKey().getAccessKeyId(), paascloudProperties.getAliyun().getKey().getAccessKeySecret()); DefaultProfile.addEndpoint(paascloudProperties.getAliyun().getSms().getEndpointName(), paascloudProperties.getAliyun().getSms().getRegionId(), paascloudProperties.getAliyun().getSms().getProduct(), paascloudProperties.getAliyun().getSms().getDomain()); DefaultAcsClient defaultAcsClient = new DefaultAcsClient(profile); log.info("加载SMS Bean IAcsClient OK"); return defaultAcsClient; }
Example 12
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 13
Source File: AbstractAliSmsTemplate.java From fast-family-master with Apache License 2.0 | 5 votes |
@Override public IAcsClient getAcsClient() { IClientProfile profile = DefaultProfile.getProfile(aliSmsProperties.getRegionId(), aliSmsProperties.getAliyunAccessKeyId(), aliSmsProperties.getAliyunAccessKeySecret()); try { DefaultProfile.addEndpoint(aliSmsProperties.getEndPointName(), aliSmsProperties.getRegionId(), aliSmsProperties.getProduce(), aliSmsProperties.getDomain()); } catch (ClientException e) { throw new AliSmsException(e); } return new DefaultAcsClient(profile); }
Example 14
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 15
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 16
Source File: DySmsHelper.java From jeecg-boot with Apache License 2.0 | 4 votes |
public static boolean sendSms(String phone,JSONObject templateParamJson,DySmsEnum dySmsEnum) 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); //验证json参数 validateParam(templateParamJson,dySmsEnum); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(phone); //必填:短信签名-可在短信控制台中找到 request.setSignName(dySmsEnum.getSignName()); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(dySmsEnum.getTemplateCode()); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam(templateParamJson.toJSONString()); //选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 //request.setOutId("yourOutId"); boolean result = false; //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); logger.info("短信接口返回的数据----------------"); logger.info("{Code:" + sendSmsResponse.getCode()+",Message:" + sendSmsResponse.getMessage()+",RequestId:"+ sendSmsResponse.getRequestId()+",BizId:"+sendSmsResponse.getBizId()+"}"); if ("OK".equals(sendSmsResponse.getCode())) { result = true; } return result; }
Example 17
Source File: SmsService.java From microservice-recruit with Apache License 2.0 | 4 votes |
private IAcsClient getAcsClient() throws ClientException { IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN); return new DefaultAcsClient(profile); }
Example 18
Source File: AliyunSmsSender.java From cola with MIT License | 4 votes |
@Override public SmsSendResult send(SmsParameter parameter) { SmsSendResult result = new SmsSendResult(); result.setSuccess(true); // 短信API产品名称(短信产品名固定,无需修改) final String domain = "dysmsapi.aliyuncs.com"; // 短信API产品域名(接口地址固定,无需修改) // 替换成你的AK // 初始化ascClient,暂时不支持多region(请勿修改) IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", domain); IAcsClient acsClient = new DefaultAcsClient(profile); // 组装请求对象 SendSmsRequest request = new SendSmsRequest(); // 使用post提交 // 必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 request.setPhoneNumbers(StringUtils.join(parameter.getPhoneNumbers(), ",")); // 必填:短信签名-可在短信控制台中找到 request.setSignName(parameter.getSignName()); // 必填:短信模板-可在短信控制台中找到 request.setTemplateCode(parameter.getTemplateCode()); // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${credential}"时,此处的值为 // 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 request.setTemplateParam(parameter.getParams()); //request.setOutId("yourOutId"); // 请求失败这里会抛ClientException异常 SendSmsResponse sendSmsResponse = null; try { sendSmsResponse = acsClient.getAcsResponse(request); } catch (Exception e) { result.setSuccess(false); result.setCode("SEND_SMS_FAILURE"); throw new RuntimeException("发送短信发生错误:" + e); } String okStatus = "OK"; if (sendSmsResponse.getCode() == null || !okStatus.equals(sendSmsResponse.getCode())) { log.error("发送短信失败:" + sendSmsResponse.getMessage()); result.setSuccess(false); result.setCode(sendSmsResponse.getCode()); return result; } log.info("发送短信成功:" + sendSmsResponse.getCode()); return result; }
Example 19
Source File: AliyunPushUtil.java From xnx3 with Apache License 2.0 | 4 votes |
public AliyunPushUtil(String regionId, String accessKeyId, String accessKeySecret, long appKey) { this.appKey = appKey; profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); client = new DefaultAcsClient(profile); }
Example 20
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; }