引入依赖
<dependency><groupId>com.aliyungroupId><artifactId>aliyun-java-sdk-coreartifactId><version>4.5.3version>dependency><dependency><groupId>com.aliyungroupId><artifactId>aliyun-java-sdk-dysmsapiartifactId><version>1.1.0version>dependency><dependency><groupId>com.aliyungroupId><artifactId>dysmsapi20170525artifactId><version>2.0.1version>dependency><dependency><groupId>com.aliyungroupId><artifactId>tea-openapiartifactId><version>0.0.10version>dependency><dependency><groupId>com.aliyungroupId><artifactId>tea-consoleartifactId><version>0.0.1version>dependency><dependency><groupId>com.aliyungroupId><artifactId>tea-utilartifactId><version>0.2.10version>dependency><dependency><groupId>com.aliyungroupId><artifactId>teaartifactId><version>[1.0.3, 2.0.0)version>dependency>
SmsUtils
发送短信工具类
package com.hxecm.util;import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.*;
import com.hxecm.dto.AlarmMsgDto;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SmsUtils {private static final String ACCESS_KEY_ID &#61; "ACCESS_KEY_ID";private static final String ACCESS_KEY_SECRET &#61; "ACCESS_KEY_SECRET";private static final String Sign_Name &#61; "Sign_Name";private static final String Ver_Template_Code &#61; "Ver_Template_Code";private static final String Notice_Template_Code &#61; "Notice_Template_Code";public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {Config config &#61; new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);config.endpoint &#61; "dysmsapi.aliyuncs.com";return new com.aliyun.dysmsapi20170525.Client(config);}private static int newCode;public static int getNewCode() {return newCode;}public static void setNewCode() {newCode &#61; (int) ((Math.random() * 9 &#43; 1) * 1000);}public static SendSmsResponse sendSMS(String tel,String smsCode) throws Exception {com.aliyun.dysmsapi20170525.Client client &#61; SmsUtils.createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);SendSmsRequest sendSmsRequest &#61; new SendSmsRequest().setPhoneNumbers(tel).setSignName(Sign_Name).setTemplateCode(Ver_Template_Code).setTemplateParam("{\"code\":\"" &#43; smsCode &#43; "\"}");SendSmsResponse resp &#61; client.sendSms(sendSmsRequest);return resp;}public static String getSmsCode(){setNewCode();String smsCode &#61; Integer.toString(getNewCode());return smsCode;}public static SendSmsResponse sendSMSNotification(String phone, AlarmMsgDto alarmMsgDto) throws Exception {com.aliyun.dysmsapi20170525.Client client &#61; SmsUtils.createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);SendSmsRequest sendSmsRequest &#61; new SendSmsRequest().setPhoneNumbers(phone).setSignName(Sign_Name).setTemplateCode(Notice_Template_Code).setTemplateParam("{\"context\":\"context\"}");SendSmsResponse resp &#61; client.sendSms(sendSmsRequest);return resp;}}
SendSmsResponse ,是否发送成功检验
if (resp.getBody().getCode() !&#61; null && resp.getBody().getCode().equals("OK")) {log.info("SUCCESS");} else {log.info("ERROR");}