本文为您介绍了Java调用SendMessageToGlobe发送短信。
请求参数
名称 | 类型 | 是否必选 | 示例值 | 描述 |
---|---|---|---|---|
To | String | 是 | 452220121 | 每次只支持一个号码调用发送,号码格式为:Country Code+ Phone Number。 |
From | String | 否 | abc12345 | 支持SenderId的发送,只允许支持数字+字母,含有字母数字的最长11位,纯数字支持15位。 |
Message | String | 是 | 你好 | 短信的完整内容。 |
返回数据
名称 | 类型 | 示例值 | 描述 |
---|---|---|---|
MessageId | String | 10080303003003 | 发送回执ID,可根据该ID查询具体的发送状态。 |
To | String | 6531177810 | 发送号码。 |
From | String | Alicloud | 目前支持SenderId,返回传入的SenderId。 |
ResponseCode | String | OK | 状态码-返回OK代表请求成功,其他错误码详见错误码列表。 |
ResponseDescription | String | The SMS Send Request was accepted | 状态码的描述。 |
Segments | String | 1 | 短信的计费条数。 |
NumberDetail | String | 号码的详细属性。 | |
└Country | String | Hongkong, China | 号码所属国家。 |
└Region | String | HongKong | Number Region。 |
└Carrier | String | CMI | 号码所属的运营商网络。 |
引入阿里云核心包
- 方法一:maven方法。
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.6</version> </dependency>
- 方法二:core-sdk下载地址:Java API SDK。
调用示例
注意事项如下:
- 构造DefaultProfile时,第一个参数(regionId)必须为ap-southeast-1,请勿修改。
- domain必须为dysmsapi.ap-southeast-1.aliyuncs.com,请勿修改。
- version必须为2018-05-01,请勿修改。
package com.alicom.dysms.api;
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.4.6</version>
</dependency>
*/
public class CommonRpc {
public static void main(String[] args) {
//初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-1", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
//域名,请勿修改
request.setDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
//API版本号,请勿修改
request.setVersion("2018-05-01");
//API名称
request.setAction("SendMessageToGlobe");
//接收号码,格式为:国际码+号码,必填
request.putQueryParameter("To", "6212345678901");
//发送方senderId,选填
//request.putQueryParameter("From", "1234567890");
//短信内容,必填
request.putQueryParameter("Message", "have a test.");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}