package com.mzl.flower.utils; import com.alibaba.fastjson.JSON; 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 lombok.extern.slf4j.Slf4j; @Slf4j public class SmsUtil { //产品名称:云通信短信API产品,开发者无需替换 static final String product = "Dysmsapi"; //产品域名,开发者无需替换 static final String domain = "dysmsapi.aliyuncs.com"; static final String connectTime = "30000"; static final String readTime = "30000"; static final String keyId = "LTAI5tRr7uLjPPxzGMJPH6fz"; static final String keySecret = "GiOamDfkVP4TWiNnuSptyGQLuMRBMG"; static final String signName = "苏州云游四方信息科技"; public static SendSmsResponse sendSms(final String tel, final String templateCode, Object paramMap) throws ClientException { return sendSms(tel, templateCode, JSON.toJSONString(paramMap)); } public static SendSmsResponse sendSms(final String tel, final String templateCode, final String templateParam) throws ClientException { return sendSms(tel, templateCode, templateParam, null); } public static SendSmsResponse sendSms(final String tel, final String templateCode, final String templateParam, final String outId) throws ClientException { log.info("Send SMS [mobile no:" + tel + " templateCode:" + templateCode + " templateParam:" + templateParam + "]"); System.setProperty("sun.net.client.defaultConnectTimeout", connectTime); System.setProperty("sun.net.client.defaultReadTimeout", readTime); IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", keyId, keySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); request.setPhoneNumbers(tel); request.setSignName(signName); request.setTemplateCode(templateCode); request.setTemplateParam(templateParam); request.setOutId(outId); SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); log.info("Send SMS [mobile no:" + tel + " templateCode:" + templateCode + " templateParam:" + templateParam + "] result: " + sendSmsResponse.getCode() + " " + sendSmsResponse.getMessage()); return sendSmsResponse; } }