JAVA调用阿里云短信接口
官方文档:https://help.aliyun.com/document_detail/101414.html?spm=a2c4g.11186623.6.626.2cef6220yxh5l7
jar包下载地址:https://yvioo.lanzous.com/b00nos2le
密码:383m
maven依赖
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.0.6</version> <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 --> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.1.0</version> </dependency>
Message.java 短信实体类
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
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 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") @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); } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)