阿里云的短信服务acsClient+java

1. 开通aliyun上的短信服务,主要得到

(1)访问秘钥 accessKeyId <==>AccessKeySecret
(2)短信签名+模板code
参考官方文档

2. API测试短信发送:

参考官方文档
(1)下载sdk (原版)

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.3</version>
        </dependency>

(2) 测试

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.16</version>
</dependency>
*/
    public  void sendSms( ) {
        // 1. 初始化acsClient
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); 
        IAcsClient client = new DefaultAcsClient(profile);
        // 2. 组装请求对象
        /***
       http(s)://dysmsapi.aliyuncs.com/?Action=SendSms
	   &PhoneNumbers=1381111*****
       &SignName=阿里云
       &TemplateCode=SMS_1530****
       &<公共请求参数>
		***/
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");  // url
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        /*****/
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);   // 待发送手机号
        request.putQueryParameter("SignName", "aliyun");  // 短信签名
        request.putQueryParameter("TemplateCode", "SMS_200191424"); // 短信模板code
        /**填充短信模板**/
        Map<String, String> params = new HashMap<>();
        params.put("station", "station");
        params.put("channel", "channel");
        params.put("alarm", "alarm");
        params.put("message", "message");
        request.putQueryParameter("TemplateParam", JSON.toJSONString(params)); // 短信模板变量对应的实际值
       	// 3. 发送请求
        try {
            CommonResponse response = client.getCommonResponse(request); 
            log.info(response.getData());
        } catch (Exception e) {
            log.error("短信发送失败:", e);
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值