Java对接阿里云境外港澳台短信示例
Java对接阿里云境外/港澳台短信发送服务
·
最近接到一个需求要使用到阿里云境外短信,所以就试着捣鼓了一下,目前网上相关资料还是比较少的,官方文档又很不官方,所以就整理了一份
首先还是一样的去阿里云控制台申请开通短信服务,这里和国内短信不一样,境外短信似乎没有那么严格,购买短信套餐后申请 ACCESS_KEY_ID 和 ACCESS_KEY_SECRET 就可以了,不要签名和短信模版,也就意味着短信内容可以自由编辑,如果你想申请模板也是可以的
当然也有不好的点,境外短信很贵,然后如果套餐包指定了地区的话你发送的手机号不在当前地区,短信是可以发送成功的,然后额外扣费,另外我在一个文档看到好像发送失败也算次数,但是一时没找到在哪个文档,你们自己多看下文档注意避坑
还有很重要的一点,境外短信这个接口是没办法发送短信到国内手机号的,加86都不行
接下来我们开始对接
我们还是先看下官方文档 阿里云官方境外短信SDK示例
这里面还能找到需要的 jar 包
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20180501</artifactId>
<version>1.0.10</version>
</dependency>
里面重点要注意的 Endpoint 配置 Endpoint 配置文档
config.endpoint = "dysmsapi.aliyuncs.com"; // 这个就不行
config.endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com"; // 换这个
Endpoint 错了会提示503,就是下面这个异常
code: 503, The request has failed due to a temporary failure of the server. request id: 0A8643E7-0C7C-594F-9466-CF6644995936
https://api.aliyun.com/troubleshoot?q=ServiceUnavailable&product=Dysmsapi&requestId=0A8643E7-0C7C-594F-9466-CF6644995936
那个链接点进去是下面这样的,因为短信服务不是我申请的所以看不到具体原因
还有一个就是不同国家电话需要加上区号,也就是电话代码 电话代码文档
我们稍微整理一下上面的方法,注意 AccessKeyId 和 AccessKeySecret 建议放到配置文件或其它更安全的地方,我这里方便测试直接写死在代码里面了,一般不推荐这么干
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20180501.models.SendMessageToGlobeResponse;
import com.hub.common.exception.BusinessException;
import com.aliyun.dysmsapi20180501.models.SendMessageToGlobeRequest;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aliyun.tea.TeaException;
import lombok.extern.java.Log;
@Log
public class AliYunJWSMSUtil {
private static volatile com.aliyun.dysmsapi20180501.Client client;
public static com.aliyun.dysmsapi20180501.Client getClient() throws Exception {
if (client == null) {
synchronized (AliYunJWSMSUtil.class) {
if (client == null) {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId("123456")
.setAccessKeySecret("123456");
config.endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com";
client = new com.aliyun.dysmsapi20180501.Client(config);
}
}
}
return client;
}
public static Boolean sendMsg(String to, String msg) {
try {
com.aliyun.dysmsapi20180501.Client client = getClient();
SendMessageToGlobeRequest request = new SendMessageToGlobeRequest()
.setTo(to)
.setMessage(msg);
RuntimeOptions runtime = new RuntimeOptions();
SendMessageToGlobeResponse response = client.sendMessageToGlobeWithOptions(request, runtime);
if (response != null && response.getBody() != null && "OK".equals(response.getBody().getResponseCode())) {
return true;
} else {
log.info("短信发送失败: " + JSONObject.toJSONString(response));
throw new BusinessException("短信验证码发送失败,请稍后重试");
}
} catch (TeaException e) {
log.info("短信发送失败: " + e.getMessage());
throw new BusinessException("短信服务异常,请稍后重试");
} catch (Exception e) {
log.info("短信发送失败: " + e.getMessage());
throw new BusinessException("短信发送失败,请联系管理员");
}
}
public static void main(String[] args_) throws Exception {
// 比如香港的区号852 然后手机号12345678 那么to就是85212345678
sendMsg("85212345*****", "祝您度过愉快的一天!");
}
}
发送成功会返回下面这样的数据结构
{
"headers": {
"access-control-allow-origin": "*",
"date": "Sat, 22 Mar 2025 00:30:36 GMT",
"content-length": "253",
"keep-alive": "timeout=25",
"x-acs-request-id": "7CAA23EB-6DA7-3BD2-9C15-123456",
"connection": "keep-alive",
"content-type": "application/json;charset=utf-8",
"etag": "2eLYMicRgAhzdN9sJ8kjFbg3",
"access-control-expose-headers": "*",
"x-acs-trace-id": "0fbb552a9fbc32b24157bac401233456"
},
"body": {
"numberDetail": {
"country": "Venezuela",
"carrier": "",
"region": "Caracas/Miranda/Vargas"
},
"responseDescription": "OK",
"requestId": "7CAA23EB-6DA7-3BD2-9C15-123456",
"messageId": "612131742603436253",
"to": "58212547****",
"responseCode": "OK",
"segments": "1"
}
}
更多推荐
所有评论(0)