Java Code Examples for com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse#getCode()

The following examples show how to use com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse#getCode() . 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: SmsService.java    From microservice-recruit with Apache License 2.0 6 votes vote down vote up
private boolean sendSms(String phoneNumber, String randomCode) {
    SendSmsRequest request = new SendSmsRequest();
    request.setMethod(MethodType.POST);
    request.setPhoneNumbers(phoneNumber);
    request.setSignName("leader直聘");
    request.setTemplateCode("SMS_130924380");
    request.setTemplateParam("{\"code\":\"" + randomCode + "\"}");
    try {
        SendSmsResponse sendSmsResponse = getAcsClient().getAcsResponse(request);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals(Constant.OK)) {
            log.info("发送短信成功");
            return true;
        }
        log.error(sendSmsResponse.getCode());
    } catch (ClientException e) {
        log.error("ClientException异常:" + e.getMessage());
        throw new MyException(ResultEnum.SEND_NOTE_ERROR);
    }
    log.error("发送短信失败");
    return false;
}
 
Example 2
Source File: SmsService.java    From microservice-recruit with Apache License 2.0 6 votes vote down vote up
private boolean sendSms(String phoneNumber, String randomCode) {
    SendSmsRequest request = new SendSmsRequest();
    request.setMethod(MethodType.POST);
    request.setPhoneNumbers(phoneNumber);
    request.setSignName("leader直聘");
    request.setTemplateCode("SMS_130924380");
    request.setTemplateParam("{\"code\":\"" + randomCode + "\"}");
    try {
        SendSmsResponse sendSmsResponse = getAcsClient().getAcsResponse(request);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals(Constant.OK)) {
            log.info("发送短信成功");
            return true;
        }
        log.error(sendSmsResponse.getCode());
    } catch (ClientException e) {
        log.error("ClientException异常:" + e.getMessage());
        throw new MyException(ResultEnum.SEND_NOTE_ERROR);
    }
    log.error("发送短信失败");
    return false;
}
 
Example 3
Source File: AliSmsService.java    From faster-framework-project with Apache License 2.0 6 votes vote down vote up
@Override
public boolean send(SendSmsRequest request) {
    try {
        IAcsClient acsClient = new DefaultAcsClient(profile);
        request.setMethod(MethodType.POST);
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().
                equals("OK")) {
            return true;
        }
    } catch (ClientException e) {
        log.error(e.getMessage());
        return false;
    }
    return false;
}
 
Example 4
Source File: SmsLogServiceImpl.java    From mall4j with GNU Affero General Public License v3.0 5 votes vote down vote up
private void sendSms(String mobile, String templateCode, Map<String, String> params) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aLiDaYu.getAccessKeyId(), aLiDaYu.getAccessKeySecret());
        DefaultProfile.addEndpoint("cn-hangzhou", PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(mobile);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(aLiDaYu.getSignName());
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        request.setTemplateParam(Json.toJsonString(params));


        //hint 此处可能会抛出异常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        log.debug(Json.toJsonString(sendSmsResponse));
        if (sendSmsResponse.getCode() == null || !SEND_SMS_SUCCESS_FLAG.equals(sendSmsResponse.getCode())) {
            throw new YamiShopBindException("发送短信失败,请稍后再试:" + sendSmsResponse.getMessage());
        }
    }
 
Example 5
Source File: AliyunUtil.java    From roncoo-education with MIT License 5 votes vote down vote up
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: AliSMSServiceImpl.java    From sms with MIT License 5 votes vote down vote up
private void checkChannelException(SendSmsResponse sendSmsResponse) throws ChannelException {
	String code = sendSmsResponse.getCode();
	if (LIMIT_CONTROL.equals(code)) {
		throw new BaseException(BaseResultEnum.SEND_TOO_FREQUENTLY, "短信发送过于频繁," + sendSmsResponse.getMessage());
	} else if (OUT_OF_SERVICE.equals(code) || AMOUNT_NOT_ENOUGH.equals(code)) {
		throw new AmountNotEnoughException();
	}
}
 
Example 7
Source File: SimaplePhoneRegist.java    From danyuan-application with Apache License 2.0 5 votes vote down vote up
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 8
Source File: AliYunMsgSender.java    From WePush with MIT License 5 votes vote down vote up
@Override
public SendResult send(String[] msgData) {
    SendResult sendResult = new SendResult();

    try {
        //初始化acsClient,暂不支持region化
        SendSmsRequest sendSmsRequest = aliyunMsgMaker.makeMsg(msgData);
        sendSmsRequest.setPhoneNumbers(msgData[0]);
        if (PushControl.dryRun) {
            sendResult.setSuccess(true);
            return sendResult;
        } else {
            SendSmsResponse response = iAcsClient.getAcsResponse(sendSmsRequest);
            if (response.getCode() != null && "OK".equals(response.getCode())) {
                sendResult.setSuccess(true);
            } else {
                sendResult.setSuccess(false);
                sendResult.setInfo(response.getMessage() + ";ErrorCode:" + response.getCode());
            }
        }
    } catch (Exception e) {
        sendResult.setSuccess(false);
        sendResult.setInfo(e.getMessage());
        log.error(ExceptionUtils.getStackTrace(e));
    }

    return sendResult;
}
 
Example 9
Source File: UKTools.java    From youkefu with Apache License 2.0 4 votes vote down vote up
/**
 * 发送短信
 * @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 10
Source File: AliyunSmsSender.java    From cola with MIT License 4 votes vote down vote up
@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 11
Source File: MsgSend.java    From mall with MIT License 4 votes vote down vote up
public static void send(String phone,String code) {
 	try {
  	//设置超时时间-可自行调整
  	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 = "LTAIMsRKCwywpzbG5544";//你的accessKeyId,参考本文档步骤2
  	final String accessKeySecret = "MdzaNFoY9W5CxYAme1EhsaYcgUTFKt6655ee";//你的accessKeySecret,参考本文档步骤2
  	//初始化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个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
  	 request.setPhoneNumbers(phone);
  	 //必填:短信签名-可在短信控制台中找到
  	 request.setSignName("阿里云短信测试专用测试");;
  	 //必填:短信模板-可在短信控制台中找到
  	 request.setTemplateCode("SMS_13839005045");
  	 //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
  	 //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
  	 request.setTemplateParam("{\"code\":\""+code+"\"}");
  	 //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
  	 //request.setSmsUpExtendCode("90997");
  	 //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
  	 //request.setOutId("yourOutId");
  	//请求失败这里会抛ClientException异常
  	SendSmsResponse sendSmsResponse;
sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
   	//请求成功
   }
 	 }catch(Exception ex) {
logger.error(ex.getMessage(),ex);
 	 }
 	
 }
 
Example 12
Source File: SmsapiService.java    From FlyCms with MIT License 4 votes vote down vote up
/**
 * 手机注册验证码发送并保存
 * 
 * @param PhoneNumber
 *        要发送的手机号码
 * @param code
 *        指定发送的验证码
  * @param type
  *        1手机注册验证码,2安全手机设置验证码,3密码重置验证码
 * @return
 * @throws ClientException
 */
 @Transactional
public boolean mobileRegisterCode(String PhoneNumber,String code,int type) throws ClientException, InterruptedException{
     Smsapi api=smsapiDao.findSmsapiByid(1);
     String template=null;
     if(type==1){
         template=api.getRegCode();
     }else if(type==2){
         template=api.getSafeCode();
     }else if(type==3){
         template=api.getResetCode();
     }else{
         template=api.getRegCode();
     }
    SendSmsResponse response = sendSms(PhoneNumber,code,api.getAccessKeyId(), api.getAccessKeySecret(),api.getSignName(),template);
    Thread.sleep(3000L);
    //查明细
    if(response.getCode() != null && response.getCode().equals("OK")) {
        QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(PhoneNumber,response.getBizId(),api.getAccessKeyId(), api.getAccessKeySecret());
        int i = 0;
        System.out.println("短信明细查询接口返回数据----------------");
        //查询发送状态
        for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs())
        {
            System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
            //发送成功状态为1则发送成功
            System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
            if(smsSendDetailDTO.getSendStatus()==1) {
                UserActivation activation=new UserActivation();
                SnowFlake snowFlake = new SnowFlake(2, 3);
                activation.setId(snowFlake.nextId());
                activation.setUserName(PhoneNumber);
                activation.setCode(code);
                activation.setCodeType(type);
                activation.setReferStatus(0);
                activation.setReferTime(new Date());
                userDao.addUserActivation(activation);
                return true;
            }
        }
    }
	return false;
}