1.购买阿里云短信服务
2.申请签名
3.申请短信模板
4.获取密钥
5.maven依赖
<dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi0525</artifactId><version>2.0.9</version></dependency>
6.发送短信工具类
package com.thk.utils;import com.aliyun.dysmsapi0525.models.SendSmsRequest;import com.aliyun.teaopenapi.models.Config;import com.aliyun.teautil.models.RuntimeOptions;import com.thk.constant.Constant;import org.ponent;@Componentpublic class Sample {/*** 使用AK&SK初始化账号Client** @return Client* @throws Exception*/public static com.aliyun.dysmsapi0525.Client createClient() throws Exception {Config config = new Config()// 您的AccessKey ID.setAccessKeyId(Constant.ACCESSKEYID)// 您的AccessKey Secret.setAccessKeySecret(Constant.ACCESSKEYSECRET);// 访问的域名config.endpoint = "";return new com.aliyun.dysmsapi0525.Client(config);}public void sendSms(String code) {try {com.aliyun.dysmsapi0525.Client client = Sample.createClient();SendSmsRequest sendSmsRequest = new SendSmsRequest()//签名.setSignName("申请的签名")//模板.setTemplateCode("申请的模板").setPhoneNumbers("需要发送的手机号码")//验证码.setTemplateParam("{code:+" + code + "}");RuntimeOptions runtime = new RuntimeOptions();// 复制代码运行请自行打印 API 的返回值client.sendSmsWithOptions(sendSmsRequest, runtime);System.out.println("发送成功!!!");} catch (Exception e) {e.printStackTrace();System.out.println("发送失败!!!");}}}