搜索短信,开通服务
选择国内消息,资质管理——新增资质
签名管理——添加签名,签名添加后不能修改,所以认真查看,来源最好选企事业单位,名称最好是企业全名称,前段时间因为响应工信部签名报备,移动号码发送不了短信
模板管理——添加模板,可以编写发送内容模板,字数有限制,70个字按照一条短信收费,超过之后按照67个字一条短信收费
审核都通过后可以购买短信条数,在概览——购买。新用户应该会送几条给你调试
导入依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
发送验证,phone:发送手机号,code:验证码,验证码随机生成一个缓存下来,需要与用户输入的进行比对
public static boolean sendAliyun(String phone,String code) throws Exception {
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
.accessKeyId("你的keyid")
.accessKeySecret("你的key密钥")
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
.setEndpointOverride("dysmsapi.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Parameter settings for API request
JSONObject jsonObject=new JSONObject();
jsonObject.put("code",code);
SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
.phoneNumbers(phone)
.signName("签名名称")
.templateCode("模板code")
.templateParam(jsonObject.toJSONString())
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
// Synchronously get the return value of the API request
SendSmsResponse resp = response.get();
LogUtil.info(new Gson().toJson(resp));
client.close();
return "OK".equalsIgnoreCase(resp.getBody().getCode()) && "OK".equalsIgnoreCase(resp.getBody().getMessage());
}
获取keyid和keysecret
进入accesskey中,创建一个就行了