This topic describes how to use the Alibaba Cloud 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 to receive the text message. Note that you must add the country code to the beginning of the mobile phone number.
From String No XXCompany The ID of the sender. This parameter specifies the text message signature when you send the text message to Mainland China.
TemplateCode String Yes SMS_0000 The ID of the text message template. You can view the ID of the text message template in the console of Short Message Service (SMS).
TemplateParam String No {"code":"1234","product":"ytx"} The values of the variables that are included in the text message template. The values must use the JSON format. If line feeds are required in JSON-formatted data, the line feeds 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. This parameter is optional. 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 received the text message. Note that the country code is added to the beginning of the 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 the API operation returns OK, the request is successful. For more information about the other error codes, see Error codes.
ResponseDescription String The text message is received. The description of the status code.
Segments String 1 The number of the text messages that incurred fees.

Install the Alibaba Cloud SDK for Java: aliyun-java-sdk-core

  • Method 1: Add Maven dependencies.
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.4.6</version>
    </dependency>
    					
  • Method 2: Download the aliyun-java-sdk-core SDK by using the following link: Alibaba Cloud SDK for Java. This SDK allows you to call SMS API operations.

Sample requests

The considerations are described as follows:

  • When you construct the DefaultProfile class, you must use the default value of the regionId parameter. The default value is ap-southeast-1. Note that regionId is the first parameter in the DefaultProfile class.
  • You must use the default domain name: dysmsapi.ap-southeast-1.aliyuncs.com.
  • You must use the default API version number: 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 find 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);
        // Make sure that the domain name is dysmsapi.ap-southeast-1.aliyuncs.com.
        request.setDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
        // Make sure that the API version number is 2018-05-01.
        request.setVersion("2018-05-01");
        // Specify the API operation.
        request.setAction("SendMessageWithTemplate");
        // (Required) Specify the mobile phone number to receive the text message. Note that you must add the country code to the beginning of the mobile phone number.
        request.putQueryParameter("To", "8615200000000");
        request.putQueryParameter("From", "SMS signature");
        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();
        }
    }
}