1 以下代码亲测有效
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.6</version>
</dependency>
public static void main(String[] args) throws ClientException {
// TODO 一下均为官网代码:改这里
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
// 请求类型:必填 固定值
request.setMethod(MethodType.POST);
// 产品域名:必填 固定值
request.setDomain("dysmsapi.aliyuncs.com");
// 版本:必填 固定值
request.setVersion("2017-05-25");
// 动作:必填 固定值
request.setAction("SendSms");
// 必填 固定值
request.putQueryParameter("RegionId", "cn-hangzhou");
// 手机号 TODO 改这里
request.putQueryParameter("PhoneNumbers", "155********");
// 签名 TODO 改这里
request.putQueryParameter("SignName", "****");
// 模板 TODO 改这里 验证码模板只能发送[a-zA-Z0-9]
request.putQueryParameter("TemplateCode", "****");
// 发送的内容 TODO 改这里
String value = "1234";
request.putQueryParameter("TemplateParam", "{\"code\": \""+value+"\" }");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
官网地址
https://api.aliyun.com/new#/?product=Dysmsapi&api=SendSms¶ms=%7B%22RegionId%22%3A%22cn-hangzhou%22%2C%22PhoneNumbers%22%3A%22%22%2C%22SignName%22%3A%22%22%2C%22TemplateCode%22%3A%22%22%7D&tab=DEMO&lang=JAVA
错误信息具体意思
https://error-center.aliyun.com/status/product/Dysmsapi?spm=a2c1g.8271268.10000.139.6727df25PE6ooX
2 开发配置文件
package com.itl.iap.system.provider.utils;
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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* @description 短信工具类
* @author 管理员
* @date 2020/12/1 14:11
* @version 1.0
* @since JDK 1.8
*/
@Configuration
public class SmsUtils {
private static Logger logger = LoggerFactory.getLogger(SmsUtils.class);
/**
* 产品名称:云通信短信API产品,固定值Dysmsapi
*/
private static String PRODUCT;
/**
* 产品域名:固定值ysmsapi.aliyuncs.com
*/
private static String DOMAIN;
/**
* accessKey
*/
private static String ACCESS_KEY_ID;
/**
* 秘钥
*/
private static String ACCESS_KEY_SECRET;
/**
* 短信签名
*/
private static String SIGN_NAME;
/**
* 短信模板
*/
private static String VERIFICATION_CODE;
/**
* 其他模板
*/
private static String TEMPLATE_CODE1;
/**
* 注入静态变量,方便调用
*/
@Value("${aliyun.product}")
public void setProduct(String product){
PRODUCT = product;
}
@Value("${aliyun.domain}")
public void setDomain(String domain){
DOMAIN = domain;
}
@Value("${aliyun.accessKeyId}")
public void setAccessKeyId(String accessKeyId){
ACCESS_KEY_ID = accessKeyId;
}
@Value("${aliyun.accessKeySecret}")
public void setAccessKeySecret(String accessKeySecret){
ACCESS_KEY_SECRET = accessKeySecret;
}
@Value("${aliyun.signName}")
public void setSignName(String signName){
SIGN_NAME = signName;
}
@Value("${aliyun.verificationCode}")
public void setVerificationCode(String verificationCode){
VERIFICATION_CODE = verificationCode;
}
@Value("${aliyun.templateCode1}")
public void setTemplateCode1(String templateCode1){
TEMPLATE_CODE1 = templateCode1;
}
public static String getVerificationCode() {
return VERIFICATION_CODE;
}
public static String getTemplateCode1() {
return TEMPLATE_CODE1;
}
public static Boolean sendSms(String telephone, String value, String templateCode) throws ClientException {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
// 请求类型:必填 固定值
request.setMethod(MethodType.POST);
// 产品域名:必填 固定值
request.setDomain("dysmsapi.aliyuncs.com");
// 版本:必填 固定值
request.setVersion("2017-05-25");
// 动作:必填 固定值
request.setAction("SendSms");
// 必填 固定值
request.putQueryParameter("RegionId", "cn-hangzhou");
// 待发送手机号
request.putQueryParameter("PhoneNumbers", telephone);
// 短信签名-可在短信控制台中找到
request.putQueryParameter("SignName", SIGN_NAME);
// 短信模板-可在短信控制台中找到
request.putQueryParameter("TemplateCode", templateCode);
// 模板中的变量替换JSON串,如模板内容为"尊敬的用户,您的验证码为${code}"时,此处的值为
request.putQueryParameter("TemplateParam", "{\"code\":\"" + value + "\"}");
CommonResponse response = client.getCommonResponse(request);
logger.info(JSONObject.toJSONString(response));
JSONObject jsonObject = JSONObject.parseObject(response.getData());
// jsonObject = {"RequestId":"22","Message":"OK","BizId":"22^0","Code":"OK"}
if (jsonObject.getString("Code") != null && "OK".equals(jsonObject.getString("Code"))) {
logger.info("短信发送成功!");
return true;
} else {
logger.info("短信发送失败!");
}
return false;
}
/**
* 以下为测试代码,随机生成验证码
*/
public static int getNewCode() {
//每次调用生成一次四位数的随机数
return (int) (Math.random() * 899999) + 100000;
}
}
yml配置
aliyun:
product: "Dysmsapi"
domain: "ysmsapi.aliyuncs.com"
accessKeyId: "1"
accessKeySecret: "1"
signName: "1"
verificationCode: "1"
templateCode1: "1"
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/116417.html