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 java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.30</version>
</dependency>
*/
@Service
public class AliyunSmsService {
//verifyCode为验证码 根据模板选择对应的发送方式 即:1:普通模板不带验证码 2:带参数模板带验证码
public ResponseEntity aliyunSmsSend(List<String> mobileList,String verifyCode) {
String key = "AYSLCxxxxxxxxxxx";
String secret = "SaKad9R7xxxxxxxxxxx";
String regionId = "cn-qingdao";
String singName = "卓亦汇航";
String template = "SMS_0000000";
String code = "code";
DefaultProfile profile = DefaultProfile.getProfile(regionId, key, secret);
try {
for (String phone : mobileList) {
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2022-04-18");
request.setSysAction("SendSms");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", singName);
request.putQueryParameter("SendDate", new SimpleDateFormat("yyyyMMdd").format(new Date()));
request.putQueryParameter("PageSize", "50");
request.putQueryParameter("CurrentPage", "1");
request.putQueryParameter("TemplateCode", template);
if (StringUtils.isNotBlank(verifyCode)) {
String c = "{'"+code+"'";
String v = ":'"+verifyCode+"'}";
request.putQueryParameter("TemplateParam", c+v);
}
//最终发送短信方法
CommonResponse commonResponse = client.getCommonResponse(request);
System.out.println(commonResponse);
}
return ResponseEntity.ok();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return ResponseEntity.failed("发送失败");
}
}
阿里云 aliyun 发送短信demo
最新推荐文章于 2024-07-04 11:19:33 发布