All Products
Search
Document Center

:Java SDK

更新時間:Oct 10, 2023

This topic provides a comprehensive guide on how to utilize Alibaba Cloud SMS SDK for Java. By incorporating this SDK as a dependency in your application, you can easily integrate the SendMessageToGlobe operation, enabling you to send SMS messages to various regions around the world, excluding the Chinese mainland.

Usage notes

Before you use the SDK sample code, take note of the following information:

  • When you construct a new default profile by specifying the DefaultProfile parameter, the value of the regionId parameter must be ap-southeast-1 and cannot be changed. 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.

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.

  • For information about more examples, visit OpenAPI Explorer.

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