JAVA调用阿里云短信接口

jar包下载地址:https://pan.xunlew.com/s31345 

 

maven依赖


    com.aliyun
    aliyun-java-sdk-core
    4.1.0

  

Message.java

package com.test;

public class Message {
    private String mobile;
    private String signName;
    private String TemplateCode;
    private String TemplateParam;

    public String getMobile() {
        return this.mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getSignName() {
        return this.signName;
    }

    public void setSignName(String signName) {
        this.signName = signName;
    }

    public String getTemplateCode() {
        return this.TemplateCode;
    }

    public void setTemplateCode(String templateCode) {
        this.TemplateCode = templateCode;
    }

    public String getTemplateParam() {
        return this.TemplateParam;
    }

    public void setTemplateParam(String templateParam) {
        this.TemplateParam = templateParam;
    }

    public Message(String mobile, String signName, String templateCode, String templateParam) {
        this.mobile = mobile;
        this.signName = signName;
        this.TemplateCode = templateCode;
        this.TemplateParam = templateParam;
    }

    public Message() {
    }
}

  

 

SendMessageUtil.java
package com.test;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
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.aliyuncs.profile.IClientProfile;
import com.test.Message;

import java.util.Random;

public class SendMessageUtil {
    static final String product = "Dysmsapi";
    static final String domain = "dysmsapi.aliyuncs.com";
    static final String accessKeyId = "";
    static final String accessKeySecret = "";

   
    public static SendSmsResponse sendSms(Message message) throws ClientException {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        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(message.getMobile());
        request.setSignName(message.getSignName());
        request.setTemplateCode(message.getTemplateCode());
        request.setTemplateParam(message.getTemplateParam());
        SendSmsResponse sendSmsResponse = (SendSmsResponse) acsClient.getAcsResponse(request);
        return sendSmsResponse;
    }

    public static String getSixNum() {
        String str = "0123456789";
        StringBuilder sb = new StringBuilder(4);

        for (int i = 0; i < 6; ++i) {
            char ch = str.charAt((new Random()).nextInt(str.length()));
            sb.append(ch);
        }

        return sb.toString();
    }

   
}

  

Test.java 调用方法

 

@RequestMapping({"/sendRegisterMessage.jspx"})
	@ResponseBody
	public void sendMessage(String mobile, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		JSONObject object = new JSONObject();
		String code = "500";
		String msg = "";
		if (StringUtils.isBlank(mobile)) {
			code = "201";
			object.put("code", code);
			ResponseUtils.renderJson(response, "" + object);
		} else {
			JSONObject param = new JSONObject();
			param.put("code", SendMessageUtil.getSixNum());
			System.out.println("注册短信验证码:" + param.getString("code"));
			Message message = new Message(mobile, "短信签名", "短信模板ID", "" + param);

			try {
				SendSmsResponse sendSmsResponse = SendMessageUtil.sendSms(message);
				if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) {
					code = "200";
				} else {
					code = "202";
				}

				msg = sendSmsResponse.getMessage();
			} catch (Exception var11) {
				var11.printStackTrace();
			}

			object.put("code", code);
			object.put("msg", msg);
			object.put("msgCode", "" + param.get("code"));
			ResponseUtils.renderJson(response, "" + object);
		}
	}

  

 

 

js页面方法调用;https://www.cnblogs.com/pxblog/p/10529441.html

你可能感兴趣的:(JAVA调用阿里云短信接口)