阿里大鱼短信发送
Posted mozq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里大鱼短信发送相关的知识,希望对你有一定的参考价值。
阿里大鱼短信发送
官方文档
# 帮助文档
https://help.aliyun.com/product/44282.html?spm=5176.12453370.0.0.5e841cben3xsbf
# openapi在线演示
https://api.aliyun.com/new#/?product=Dysmsapi&api=SendSms&tab=DEMO&lang=JAVA
mozhuiqiu
步骤
获取accessId, accessSecret, 签名,模板id,模板参数。调用接口发送。
依赖
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
package com.mozq.ms.ms01.service;
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;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
*/
public class SendSms {
private static final String accessKeyId = "***";
private static final String accessSecret = "***";
public static void main(String[] args) {
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");
request.putQueryParameter("PhoneNumbers", "18392895235");//设置电话号码
request.putQueryParameter("SignName", "魔有追求");//设置签名
request.putQueryParameter("TemplateCode", "SMS_171112339");//设置模板
request.putQueryParameter("TemplateParam", "{code:1345666}");//设置模板参数,json格式
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
//{"Message":"OK","RequestId":"7B562B28-A4F9-417C-8CCE-E7E988EA8C20","BizId":"316818172776024297^0","Code":"OK"}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
简单案例
package com.mozq.ms.ms01.controller;
import com.mozq.ms.ms01.service.PhoneService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class LoginController {
@Autowired
private PhoneService phoneService;
@RequestMapping("/sms")
public String sms(String phone){
String code = RandomStringUtils.randomAlphanumeric(5);
boolean success = phoneService.sendCheckCode(phone, code);
return success ? "短信发送成功" : "短信发送失败";
}
}
package com.mozq.ms.ms01.service;
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.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class PhoneService {
private static final String accessKeyId = "***";//自己的
private static final String accessSecret = "***";//自己的
private static final String SignName = "魔有追求";
private static final String TemplateCode = "SMS_171112339";
public boolean sendCheckCode(String phone, String checkCode){
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");
request.putQueryParameter("PhoneNumbers", phone);//设置电话号码
request.putQueryParameter("SignName", SignName);//设置签名
request.putQueryParameter("TemplateCode", TemplateCode);//设置模板
HashMap<String, String> params = new HashMap<>();
params.put("code", checkCode);
String paramsJSON = JSONObject.toJSONString(params);
request.putQueryParameter("TemplateParam", paramsJSON);//设置模板参数,json格式
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
Map map = JSONObject.parseObject(response.getData(), Map.class);
String code = (String) map.get("Code");
if("OK".equalsIgnoreCase(code)){
return true;
}
//{"Message":"OK","RequestId":"7B562B28-A4F9-417C-8CCE-E7E988EA8C20","BizId":"316818172776024297^0","Code":"OK"}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
/*
com.aliyuncs.exceptions.ClientException: MissingPhoneNumbers : PhoneNumbers is mandatory for this action.
RequestId : BEFF0B71-305B-4640-8E13-46EE89548F47
*/
}
return false;
}
}
响应结果
{
"Message": "OK",
"RequestId": "1D05E04F-EA57-4AD9-9C5C-96772F8F2243",
"BizId": "460404072778263899^0",
"Code": "OK"
}
依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
以上是关于阿里大鱼短信发送的主要内容,如果未能解决你的问题,请参考以下文章