This topic describes how to use SMS SDK for Java to call the SendMessageWithTemplate operation. This API operation allows you to send text messages to mainland China.
Request parameters
Parameter | Type | Required | Example | Description |
---|---|---|---|---|
To | String | Yes | 45***0121 | The mobile phone number that is used to receive the text message. You must add the country code to the beginning of the mobile phone number. |
From | String | No | XXCompany | The identifier of the sender. If you send the text message to China, specify a signature. |
TemplateCode | String | Yes | SMS_0000 | The ID of the text message template. You can view the ID of the text message template in the Short Message Service (SMS) console. |
TemplateParam | String | No | {"code":"1234","product":"ytx"} | The values of the variables that are included in the text message template. The values
must be formatted in JSON.
Note If line breaks are required in JSON-formatted data, they must meet the relevant requirements
that are specified in the standard JSON protocol.
|
SmsUpExtendCode | String | No | 90999 | The extension code of the upstream text message. You can skip this parameter based on your business requirements. |
Response parameters
Parameter | Type | Example | Description |
---|---|---|---|
To | String | 45***0121 | The mobile phone number that was used to receive the text message. Format: Country code + Mobile phone number. |
MessageId | String | 100803***03003 | The ID of the text message. You can call the QueryMessage operation to query the delivery status of the text message based on the ID of the text message. |
ResponseCode | String | OK | The status code of the request. If OK is returned, the request is successful. For more information, see Error codes. |
ResponseDescription | String | Received | The description of the status code. |
Segments | String | 1 | The number of the text messages that incurred fees. |
Install the core library of Alibaba Cloud SDK for Java
- Method 1: Add Maven dependencies.
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.6</version> </dependency>
- Method 2: Click Java API SDK to download the core library of Alibaba Cloud SDK for Java.
Sample requests
Take note of the following information:
- When you define a new default profile, the value of the regionId parameter must be ap-southeast-1 and cannot be modified. Note that regionId is the first parameter in the default profile.
- You must set the domain parameter to dysmsapi.ap-southeast-1.aliyuncs.com.
- You must set the version parameter to 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) {
// Initialize the IAcsClient object. You can query the values of the <accessKeyId> and <accessSecret> parameters in the SMS console.
DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-1", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
// The domain name, which cannot be modified.
request.setDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
// The API version number, which cannot be modified.
request.setVersion("2018-05-01");
// The API operation.
request.setAction("SendMessageWithTemplate");
// The mobile phone number that is used to receive the text message. You must add the country code to the beginning of the mobile phone number.
request.putQueryParameter("To", "8615200000000");
request.putQueryParameter("From", "Alibaba Cloud Communications");
request.putQueryParameter("TemplateCode", "SMS_225001");
request.putQueryParameter("TemplateParam", "{\"code\":\"1234\"}");
request.putQueryParameter("SmsUpExtendCode", "12345");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}