This topic provides a demo to describe how to use Alibaba Cloud SDK for Java to call the SendMessageToGlobe operation. This API operation allows you to send text messages to the regions outside mainland China.

Usage notes

Before you use the SDK sample code, note 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.
  • The domain name must be dysmsapi.ap-southeast-1.aliyuncs.com and cannot be modified.
  • The API version number must be 2018-05-01 and cannot be modified.
Note The version that is used in the demo Maven dependency is a sample version. For information about the latest version of the core library, visit the Maven repository.

Sample code

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.5.17</version>
</dependency>
*/
public class CommonRpc {
    public static void main(String[] args) {
        // Initialize the AcsClient object. You can query <accessKeyId> and <accessSecret> in the Short Message Service (SMS) console.
        DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-1", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        // The domain name, which cannot be modified.
        request.setSysDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
        // The API version number, which cannot be modified.
        request.setSysVersion("2018-05-01");
        // The API operation.
        request.setSysAction("SendMessageToGlobe");
        // 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", "62123****8901");
        // Optional. Specify the ID of the sender.
        //request.putQueryParameter("From", "1234567890");
        // Required. Specify the text message content.
        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();
        }
    }
}