pom依赖
<!-- 阿里云短信SDK -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.4</version>
</dependency>
工具类
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;
import java.util.Random;
@Component
package io.renren.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import org.springframework.stereotype.Component;
@Component
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
public String sendNoteMessgae(String PhoneNumbers,String name,String template) throws Exception {
Client client = Sample.createClient("您的AccessKey ID", "您的AccessKey Secret");
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(PhoneNumbers)
.setSignName("短信签名")
.setTemplateCode(template)
.setTemplateParam("{\"name\":\""+name+"\"}");
// 复制代码运行请自行打印 API 的返回值
client.sendSms(sendSmsRequest);
return client.sendSms(sendSmsRequest).toString();
}
}
Controller
import io.renren.common.utils.SendNoteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequestMapping(value = "/api/note")
public class SendNoteController {
@Autowired
private SendNoteUtil sendNoteUtil;
@RequestMapping(value = "/sendNote",method = RequestMethod.GET)
public void sendNote(String phone, HttpServletResponse response){
String template = "";//更改为自己的短信模板
try {
response.getWriter().write(sendNoteUtil.sendNoteMessgae(phone,template));
} catch (IOException e) {
e.printStackTrace();
}
}
}