JAVA + 阿里云 实现单个短信发送 和 批量短信发送(直接拷贝就能使用)

JAVA + 阿里云 实现单个短信发送 和 批量短信发送

一、阿里云官网相关操作

1.1 秘钥获取

在这里插入图片描述
在这里插入图片描述
拿到非常非常非常重要的秘钥

1.2 签名申请

在短信服务中,找到国内消息—签名管理—添加签名, 并等待签名审核通过

1.2 模板申请

在短信服务中,找到国内消息—模板管理—添加模板, 并等待签名审核通过

附带上相关阿里会使用到的SDK链接

短信服务API参考
(单人短信发送,批量短信发送等等相关传参操作都有写):https://help.aliyun.com/document_detail/419268.html?spm=a2c4g.44282.0.0.5db43d46F1iN7T

工单提交
(遇到技术问题可以向工单中心提交):https://smartservice.console.aliyun.com/service/create-ticket?spm=5176.12818093.top-nav.ditem-0.3be916d0c5Ayjm&product=dysms

发送测试短信(可单人可批量,甚至有源码):
https://help.aliyun.com/document_detail/108064.htm?spm=a2c4g.101346.0.0.84781bceleoajG#task-2126833

二、 JAVA + 阿里云 实现单个短信发送 和 批量短信发送

2.1 maven依赖


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

2.2 发送短信

只需要根据业务需求改动红框圈出的部分. 注: 模板code必须和模板参数保持一致!!!

在这里插入图片描述


		public Boolean sendMessage() {

	        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeyId", "secret");
	        IAcsClient client = new DefaultAcsClient(profile);
	        // 构建请求:
	        CommonRequest request = new CommonRequest();
	        request.setSysMethod(MethodType.POST);
	        request.setSysDomain("dysmsapi.aliyuncs.com");
	        request.setSysVersion("2017-05-25");
	        request.setSysAction("SendSms");
	        // 自定义参数:
	        request.putQueryParameter("PhoneNumbers", "手机号");// 手机号
	        request.putQueryParameter("SignName", "申请好的签名");// 短信签名
	        request.putQueryParameter("TemplateCode", "申请好的模板code");// 短信模版CODE
	        // 构建短信验证码 必须和模板保持一致
	        Map<String,Object> map = new HashMap<>();
	        map.put("code",12138);
	        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
	        try {
	            CommonResponse response = client.getCommonResponse(request);
	            System.out.println(response.getData());
	            return response.getHttpResponse().isSuccess();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return false;
	    }
    

2.3 批量发送短信

只需要根据业务需求改动红框圈出的部分. 注: 手机号,签名名称,模板参数等改为集合形式传参,并且集合中的签名名称数量,模板参数数量必须和手机号数量保持一致!!! 并且 模板code必须和模板参数保持一致!!!

在这里插入图片描述


   		public static String sendSmsBatch() {
           DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeyId", "secret");
           IAcsClient client = new DefaultAcsClient(profile);
   
           CommonRequest request = new CommonRequest();
           request.setMethod(MethodType.POST);
           request.setDomain("dysmsapi.aliyuncs.com");
           request.setVersion("2017-05-25");
           request.setAction("SendBatchSms");
           request.putQueryParameter("RegionId", "cn-hangzhou");
           request.putQueryParameter("PhoneNumberJson","[\"手机号1\",\"手机号2\"]");
           request.putQueryParameter("SignNameJson", "[\"签名1\",\"签名2\"]");
           request.putQueryParameter("TemplateParamJson", "[{\"code\":\"模板1\"},{\"code\":\"模板2\"}]");
           request.putQueryParameter("TemplateCode", "SMS_460565185");
           CommonResponse response= null;
           try {
               response = client.getCommonResponse(request);
               System.out.println(response.getData());
           } catch (ServerException e) {
               e.printStackTrace();
           } catch (ClientException e) {
               e.printStackTrace();
           }
           return response.getData();
       }
   
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值