JAVA----发送短信息工具类

发送短信息工具类

以阿里平台为准

加入sdk

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>dysmsapi20170525</artifactId>
    <version>[2.0.0,3.0.0)</version>
</dependency>
新版本
@Slf4j
public class AliSmsUtils {
	// 登录阿里控制台获取
	private final static String accessKeyId = AliSmsConstant.ACCESS_KEY_ID;
    private final static String accessKeySecret = AliSmsConstant.ACCESS_KEY_SECRET;


    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);

        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


    /**
     * send single
     * @param mobile
     * @param signName 签名名称
     * @param templateCode  如:SMS_156150027
     * @param param "{\"code\":\"123456\"}"
     * @return
     * @throws Exception
     */
    public static AjaxResult send(String mobile, String signName, String templateCode, String param) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = AliSmsUtils.createClient(accessKeyId, accessKeySecret);
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(mobile)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam(param);

        SendSmsResponse response = client.sendSms(request);

        if (!Objects.isNull(response) && response.getBody().code.equals("OK") && response.getBody().message.equals("OK")) {
            log.info("[阿里短信]----发送成功");
            return AjaxResult.success("", response.getBody());
        }

        log.error("[阿里短信]----发送失败, message:{}", response.getBody().getMessage());
        return AjaxResult.error(-9, "",response.getBody());
    }

}
对比旧版本
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.30</version>
</dependency>

@Slf4j
public class AliSmsUtils {
	    public static void send(String mobile) {
        DefaultProfile profile = DefaultProfile.getProfile("ap-northeast-1", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", "阿里大于测试专用");
        request.putQueryParameter("TemplateCode", "SMS_209335004");
        request.putQueryParameter("TemplateParam", "{\"code\":\"123456\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值