阿里云短信发送接口

一:导入maven配置:

 


            com.aliyun
            aliyun-java-sdk-core
            4.1.0
        
        
            com.aliyun
            aliyun-java-sdk-dysmsapi
            1.1.0
        

二:直接发送短信:

package com.koala.console.apps.visitor.utils;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendBatchSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendBatchSmsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.common.collect.Lists;
import com.koala.console.apps.visitor.constant.VisitorConst;
import com.koala.console.common.configuration.VarConfiguration;

import java.util.List;

/**
 * @author gongyiyang
 * @Date: 19-7-3 16:16
 * @Description:
 */
public class SmsSendtool {

    /**
     *单条发送
     */
    public static SendSmsResponse sendSmsDetail(String phone, Integer codelen) throws ClientException {
        JSONObject templateParam = new JSONObject();
        String code = RandomUtil.randomString(codelen);
        templateParam.put("code", code);
        DefaultProfile profile = DefaultProfile.getProfile(VisitorConst.regionId, VarConfiguration.accessKeyId, VarConfiguration.accessKey);
        DefaultAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest smsRequest = new SendSmsRequest();
        smsRequest.setPhoneNumbers(phone);
        smsRequest.setTemplateCode(VisitorConst.templateCode);
        smsRequest.setTemplateParam(templateParam.toString());
        smsRequest.setSignName(VisitorConst.signName);
        smsRequest.setEndpoint(VisitorConst.endpoint);
        return acsClient.getAcsResponse(smsRequest);
    }

    /**
     *批量发送短信
     */
    public static SendBatchSmsResponse sendSmsBatch(List phoneJson, Integer codelen) throws ClientException {
        JSONObject templateParam = new JSONObject();
        String code = RandomUtil.randomString(codelen);
        templateParam.put("code", code);
        DefaultProfile profile = DefaultProfile.getProfile(VisitorConst.regionId, VarConfiguration.accessKeyId, VarConfiguration.accessKey);
        DefaultAcsClient acsClient = new DefaultAcsClient(profile);
        SendBatchSmsRequest smsRequest = new SendBatchSmsRequest();
        smsRequest.setPhoneNumberJson(pinStr(phoneJson));
        smsRequest.setTemplateCode(VisitorConst.templateCode);
        smsRequest.setTemplateParamJson(copypar(phoneJson,templateParam).toString());
        smsRequest.setSignNameJson(pinStr(copypar(phoneJson,VisitorConst.signName)));
        smsRequest.setEndpoint(VisitorConst.endpoint);
        return acsClient.getAcsResponse(smsRequest);
    }

    public static String pinStr(List valve) {
        StringBuilder str = new StringBuilder("[");
        valve.forEach(k ->
                str.append("\"").append(k).append("\"").append(",")
        );
        str.append("]");
        return str.delete(str.length() - 2, str.length() - 1).toString();
    }

    private static List copypar(List phoneJson, Object param){
        List arrayList = Lists.newArrayList();
        phoneJson.forEach(k->arrayList.add(param));
        return arrayList;
    }
}
 
  

阿里云短信发送接口_第1张图片

阿里云短信发送接口_第2张图片

阿里云短信发送接口_第3张图片

https://help.aliyun.com/knowledge_detail/106420.html

阿里云短信发送接口_第4张图片

阿里云短信发送接口_第5张图片

 

-------------------------------------------------------------------

最后分享一个买东西省钱又可以分享赚钱的APP:

阿里云短信发送接口_第6张图片

你可能感兴趣的:(java)