阿里云短信发送工具类
前期准备
开通阿里云短信服务
申请签名管理和模板管理
获取Access_key和Access_secret
鼠标放在右上角头像上会出现
发送短信代码部分
发送短信 pom 相关依赖
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-dysmsapi -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
发送短信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;
/**
* 短信API产品的DEMO程序,工程中包含了一个SmsDemo类,直接通过
* 执行main函数即可体验短信产品API功能(只需要将AK替换成开通了云通信-短信产品功能的AK即可)
* <p>
* 备注:Demo工程编码采用UTF-8
* 国际短信发送请勿参照此DEMO
*/
public class SendMessageDemo {
//产品名称:云通信短信API产品,开发者无需替换
static final String product = "Dysmsapi";
//产品域名,开发者无需替换
static final String domain = "dysmsapi.aliyuncs.com";
// 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
static final String accessKeyId = "";
static final String accessKeySecret = "";
public static SendSmsResponse sendSms(String phone, String code) throws ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
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(phone);
//必填:短信签名-可在短信控制台中找到
request.setSignName("");
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode("");
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam("{\"code\":\"" + code + "\"}");
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request.setOutId("yourOutId");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
public static void main(String[] args) throws ClientException, InterruptedException {
//发短信
SendSmsResponse response = sendSms("12312341234", "7865");
System.out.println("短信接口返回的数据----------------");
System.out.println("Code = " + response.getCode());
System.out.println("Message = " + response.getMessage());
System.out.println("RequestId = " + response.getRequestId());
System.out.println("BizId = " + response.getBizId());
}
}
发送短信成功
提供生成6位验证码的工具
验证码 pom 相关依赖
<!-- 随机数生成https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
验证码Java代码
import org.apache.commons.lang3.RandomStringUtils;
public class GetRandomUtil {
//产生验证码
public static String randomCode() {
String verifyCode = RandomStringUtils.randomNumeric(6);
return verifyCode;
}
}
发送短信调用类
import com.capgemini.domain.User;
import com.capgemini.util.CommonUtil;
import com.capgemini.util.GetRandomUtil;
import com.capgemini.util.SendMessage;
import com.capgemini.vo.BaseResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping
@CrossOrigin
public class SmsController {
@PostMapping("/sms")
public ResponseEntity<Object> sendSms(@RequestBody User user) {
// 通过GetRandomCodeUtil生成随机验证码
String code = GetRandomUtil.randomCode();
BaseResult br = null;
try {
//手机号和验证码进行发短信
SendMessage.sendSms(user.getPhone(), code);
br = new BaseResult(CommonUtil.SUCCESS, "发送成功", code);
return ResponseEntity.ok(br);
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<>(HttpStatus.OK);
}
}