This topic describes how to use SMS SDK for Java to call the SendMessageToGlobe operation. This API operation allows you to send text messages to regions outside mainland China.

Request parameters

Parameter Type Required Example Description
To String Yes 452220121 You can call the SendMessageToGlobe operation to send text messages to only one mobile phone number. You must add the country code to the beginning of the mobile phone number.
From String No abc12345 You can specify the sender ID when you call the API operation. The sender ID can contain digits and letters. If the sender ID contains only letters, the sender ID can be a maximum of 11 characters in length. If the sender ID contains only digits, the sender ID can be a maximum of 15 characters in length.
Message String Yes Hello The content of the text message.

Response parameters

Parameter Type Example Description
MessageId String 10080303003003 The ID of the delivery receipt. You can query the status of the text message based on this parameter.
To String 6531177810 The mobile phone number that received the text message.
From String Alicloud The sender 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 The SMS Send Request was accepted The description of the status code.
Segments String 1 The number of the text messages that incurred fees.
NumberDetail String The details of the mobile phone number.
Country String Hongkong, China The country to which the mobile phone number belongs.
Region String HongKong The region to which the mobile phone number belongs.
Region String CMI The carrier that provides the mobile phone number.

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("SendMessageToGlobe");
        // 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", "6212345678901");
        // 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();
        }
    }
}