com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse Java Examples

The following examples show how to use com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse. 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 vote down vote up
/**
 * 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: 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: AliyunSmsVerificationCodeSender.java    From daming with Apache License 2.0 6 votes vote down vote up
@SneakyThrows
@Override
public void send(SmsVerification verification) {
    SendSmsRequest request = new SendSmsRequest();
    request.setPhoneNumbers(verification.getMobile().getValue());
    request.setSignName(signature);
    request.setTemplateCode(templateCode);
    Map<String, String> params = new HashMap<String, String>() {
        {
            put("code", verification.getCode().getValue());
        }
    };

    request.setTemplateParam(toJson(params));

    // This data will be sent back in the confirmation. Maybe we can use it for correlation.
    // request.setOutId("");
    SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
    log.info(toJson(sendSmsResponse));
}
 
Example #4
Source File: OptSmsServiceTest.java    From paascloud-master with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: OptSmsServiceImpl.java    From paascloud-master with Apache License 2.0 6 votes vote down vote up
@Override
public SendSmsResponse sendSms(SendSmsRequest sendSmsRequest) {
	checkParam(sendSmsRequest);
	SendSmsResponse acsResponse;
	try {
		if (GlobalConstant.DEV_PROFILE.equals(profile)) {
			log.error("dev环境不发送短信");
			return new SendSmsResponse();
		}
		if (GlobalConstant.TEST_PROFILE.equals(profile)) {
			log.error("test环境不发送短信");
			return new SendSmsResponse();
		}
		acsResponse = iAcsClient.getAcsResponse(sendSmsRequest);
	} catch (ClientException e) {
		log.error("send sms message error={}", e.getMessage(), e);
		throw new OpcBizException(ErrorCodeEnum.OPC10040004, e);
	}
	log.info("send sms message OK acsResponse={}", acsResponse);
	return acsResponse;
}
 
Example #6
Source File: SmsListener.java    From leyou with Apache License 2.0 6 votes vote down vote up
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "leyou.sms.queue", durable = "true"),
        exchange = @Exchange(value = "leyou.sms.exchange",
                ignoreDeclarationExceptions = "true"),
        key = {"sms.verify.code"}))
public void listenSms(Map<String, String> msg) throws Exception {
    if (msg == null || msg.size() <= 0) {
        // 放弃处理
        return;
    }
    String phone = msg.get("phone");
    String code = msg.get("code");

    if (StringUtils.isBlank(phone) || StringUtils.isBlank(code)) {
        // 放弃处理
        return;
    }
    // 发送消息
    SendSmsResponse resp = this.smsUtils.sendSms(phone, code,
            prop.getSignName(),
            prop.getVerifyCodeTemplate());

}
 
Example #7
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 #8
Source File: SmsListener.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@RabbitListener(queues = "mogu.sms")
public void sendSms(Map<String, String> map) {

    try {
        SendSmsResponse response = smsUtil.sendSms(
                map.get("mobile"),
                map.get("template_code"),
                map.get("sign_name"),
                map.get("param"));
        System.out.println("code:" + response.getCode());
        System.out.println("message:" + response.getMessage());

    } catch (ClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
 
Example #9
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 #10
Source File: SMSUtil.java    From anyline with Apache License 2.0 6 votes vote down vote up
public SMSResult send(String sign, String template, String mobile, Map<String, String> params) { 
	SMSResult result = new SMSResult(); 
	try { 
		if(BasicUtil.isEmpty(sign)){ 
			sign = config.SMS_SIGN; 
		} 
	 	request.setPhoneNumbers(mobile); 
        //必填:短信签名-可在短信控制台中找到 
        request.setSignName(sign); 
        //必填:短信模板-可在短信控制台中找到 
        request.setTemplateCode(template); 
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 
	    request.setTemplateParam(BeanUtil.map2json(params)); 

	    //hint 此处可能会抛出异常,注意catch 
        SendSmsResponse response = client.getAcsResponse(request); 
        result.setResult(true); 
	} catch (ClientException e) { 
		e.printStackTrace(); 
		result.setCode(e.getErrCode()); 
		result.setMsg(e.getErrMsg()); 
	} 
	return result; 
}
 
Example #11
Source File: AliyunSmsMessageNotifier.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@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 #12
Source File: DySmsHelper.java    From jeewx-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 发短信接口(固定:登录验证码模板)
 * @param phone
 * @param json
 * @return
 * @throws ClientException
 */
public static boolean sendSms(String phone,JSONObject json) 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(phone);
    //必填:短信签名-可在短信控制台中找到
    request.setSignName(msgSignName);
    //必填:短信模板-可在短信控制台中找到
    request.setTemplateCode(templateCode);
    //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
    request.setTemplateParam(json.toString());
    request.setMethod(MethodType.POST);
    //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
    //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 #13
Source File: AliSmsSend.java    From charging_pile_cloud with MIT License 5 votes vote down vote up
/**
 * 发送短信
 *
 * @param ali
 * @return
 * @throws IOException
 * @throws ClientException
 */
public static AliSendBack getYZM(AliParam ali) throws IOException, ClientException {
    //发短信
    SendSmsResponse response = AliSmsUtil.sendSms(ali);
    AliSendBack sms = new AliSendBack();
    sms.setCode(response.getCode());
    sms.setMessage(response.getMessage());
    sms.setBizId(response.getBizId());
    sms.setRequestId(response.getRequestId());
    return sms;
}
 
Example #14
Source File: SmsListener.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 发送短信
 * @param message 监听到的数据
 */
@RabbitHandler
public SendSmsResponse sendSms(Map<String,String> message){
       LogBack.info("手机号:{},验证码:{};", message.get("mobile"), message.get("code"));
	try {
		return smsUtil.sendSms(message.get("mobile"), template_code, sign_name,
				"{\"code\":\"" + message.get("code") + "\"}");
       } catch (ClientException | NullPointerException e) {
           LogBack.error("发送手机验证码失败:{}", e.getMessage());
		e.printStackTrace();
	}

       return null;
}
 
Example #15
Source File: SmsUtil.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 发送短信
 *
 * @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 #16
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 #17
Source File: SmsapiService.java    From FlyCms with MIT License 5 votes vote down vote up
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 #18
Source File: AliSmsUtil.java    From charging_pile_cloud with MIT License 5 votes vote down vote up
/**
 * 发送短信类
 *
 * @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 #19
Source File: SmsAliStrategy.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
@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 #20
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 #21
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 #22
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 #23
Source File: SMSServiceImpl.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Override
public SendSmsResponse send(String id, SendSmsRequest request) throws ClientException {
    DefaultAcsClient client = clients.get(id);
    if (client == null) { throw new NullPointerException("not found sms id:" + id); }

    return client.getAcsResponse(request);// TODO async
}
 
Example #24
Source File: DySmsHelper.java    From jeewx-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 发短信接口(短信模板编码自己传入)
 * @param templateCode
 * @param phone
 * @param json
 * @return
 * @throws ClientException
 */
public static boolean sendSms(String templateCode,String phone,JSONObject json) 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(phone);
    //必填:短信签名-可在短信控制台中找到
    request.setSignName(msgSignName);
    //必填:短信模板-可在短信控制台中找到
    request.setTemplateCode(templateCode);
    //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
    request.setTemplateParam(json.toString());

    //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
    //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 #25
Source File: SmsAliStrategy.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
@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 #26
Source File: SmsListener.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 发送短信
 * @param message 监听到的数据
 */
@RabbitHandler
public SendSmsResponse sendSms(Map<String,String> message){
       LogBack.info("手机号:{},验证码:{};", message.get("mobile"), message.get("code"));
	try {
		return smsUtil.sendSms(message.get("mobile"), template_code, sign_name,
				"{\"code\":\"" + message.get("code") + "\"}");
       } catch (ClientException | NullPointerException e) {
           LogBack.error("发送手机验证码失败:{}", e.getMessage());
		e.printStackTrace();
	}

       return null;
}
 
Example #27
Source File: SmsUtil.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 发送短信
 *
 * @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 #28
Source File: SmsControllerTest.java    From staffjoy with MIT License 5 votes vote down vote up
@Test
public void testSendSms() throws ClientException {
    SendSmsResponse sendSmsResponse = new SendSmsResponse();
    sendSmsResponse.setCode("OK");
    sendSmsResponse.setRequestId("mock_id");
    when(iAcsClient.getAcsResponse(any(SendSmsRequest.class))).thenReturn(sendSmsResponse);

    String phoneNumber = "18001112222";
    String templateCode = "TESTCODE_XXXXXX";
    String templateParam = "TESTPARAM_YYYYYY";
    SmsRequest smsRequest = SmsRequest.builder()
            .to(phoneNumber)
            .templateCode(templateCode)
            .templateParam(templateParam)
            .build();
    BaseResponse baseResponse = smsClient.send(AuthConstant.AUTHORIZATION_BOT_SERVICE, smsRequest);
    assertThat(baseResponse.isSuccess()).isTrue();

    // verify sms
    ArgumentCaptor<SendSmsRequest> argument = ArgumentCaptor.forClass(SendSmsRequest.class);
    verify(iAcsClient, times(1)).getAcsResponse(argument.capture());
    SendSmsRequest sendSmsRequest = argument.getValue();
    assertThat(sendSmsRequest.getPhoneNumbers()).isEqualTo(phoneNumber);
    assertThat(sendSmsRequest.getTemplateCode()).isEqualTo(templateCode);
    assertThat(sendSmsRequest.getTemplateParam()).isEqualTo(templateParam);
    assertThat(sendSmsRequest.getSignName()).isEqualTo(appProps.getAliyunSmsSignName());

    // aliyun fail
    sendSmsResponse.setCode("FAIL");
    when(iAcsClient.getAcsResponse(any(SendSmsRequest.class))).thenReturn(sendSmsResponse);

    // even aliyun fail, send sms still succeed since async send
    baseResponse = smsClient.send(AuthConstant.AUTHORIZATION_BOT_SERVICE, smsRequest);
    assertThat(baseResponse.isSuccess()).isTrue();
}
 
Example #29
Source File: SmsUtil.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
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 #30
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());
        }
    }