This topic provides a demo to describe how to use the 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.

Considerations

Before you use the SDK sample code, pay attention to the following considerations:

  • 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.

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.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 Short Message Service (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("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();
        }
    }
}