All Products
Search
Document Center

Short Message Service:Java SDK

Last Updated:Jun 08, 2026

The Alibaba Cloud SMS SDK for Java simplifies calling SMS API operations from Java applications.

Installation

System requirements

Install with Maven

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>dysmsapi20180501</artifactId>
    <!-- Replace 'the-latest-version' with the latest version number: https://mvnrepository.com/artifact/com.aliyun/dysmsapi20180501 -->
  <version>the-latest-version</version>
</dependency>

Configure authentication

Step 1: Create a RAM user and grant permissions

Important

Your root account has full permissions. Use a RAM user for API calls and routine O&M. For more information, see Overview.

  • Create a RAM user: Go to the Create User page. Specify the required information, select Permanent AccessKey for Access Configuration, then click OK. Save your AccessKey for later use.

  • Grant permissions to the RAM User: Go to the Users page. Find the RAM user that you created and click Attach Policy in the Actions column. In the Policy search box, enter AliyunDysmsFullAccess, select the policy, and then click OK.

Note
  • AliyunDysmsFullAccess: Grants full permissions to manage the SMS service.

  • AliyunDysmsReadOnlyAccess: Grants read-only permissions to access the SMS service.

  • To create a custom policy, see RAM authorization.

Step 2: Configure access credentials

Store your AccessKey pair in environment variables. Configure environment variables on Linux, macOS, and Windows.

Note
  • Do not hard-code your AccessKey pair. Retrieve it from environment variables.

  • The sample code uses the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.

Security best practices

  • Store credentials by using environment variables or a key management service.

  • Rotate your AccessKey pair on a regular basis.

  • Follow the principle of least privilege by granting the minimum required permissions to RAM Users.

  • Do not print credential information in logs.

  • Use a RAM Role instead of your root account credentials in production environments.

Quick start

Sample code

This example calls the SMS API to send a message. Replace placeholder values as indicated in the comments.

package com.aliyun.sample;

import com.aliyun.teaopenapi.models.Config;
import com.aliyun.dysmsapi20180501.Client;
import com.aliyun.dysmsapi20180501.models.SendMessageToGlobeRequest;
import com.aliyun.dysmsapi20180501.models.SendMessageToGlobeResponse;
import static com.aliyun.teautil.Common.toJSONString;

public class Sample {
    public static Client createClient() throws Exception {
        Config config = new Config()
                // Configure your AccessKey ID. Make sure the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Configure your AccessKey secret. Make sure the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));

        // Configure the endpoint.
        config.endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com";

        return new Client(config);
    }

    public static void main(String[] args) throws Exception {
        // Initialize the request client.
        Client client = Sample.createClient();

        // Create a request object and specify the parameter values.
        SendMessageToGlobeRequest sendSmsRequest = new SendMessageToGlobeRequest()
                .setTo("<YOUR_VALUE>")
                .setMessage("<YOUR_VALUE>");

        // Obtain the response object.
        SendMessageToGlobeResponse sendSmsResponse = client.sendMessageToGlobe(sendSmsRequest);

        // The response includes the body and headers from the server.
        System.out.println(toJSONString(sendSmsResponse));
    }
}

Download the sample code

Download and run the sample code directly.

  1. Go to SendMessageToGlobe.

  2. On the Parameters tab, specify the required parameters. Example values:

  • To: 88691567****

  • Message: This is a test message.

  1. On the SDK Sample Code tab on the right, select V2.0 for the SDK version and select Java for the language (Java or Java asynchronous). Click Download Project to download the sample code.

  2. Extract the package, open the project in your IDE, wait for dependencies to load, and open src/main/java/com/aliyun/sample/Sample.java.

Run the project

Run the project. Sample response:

{
  "headers": {
    "date": "Tue, 24 Oct 2023 07:47:17 GMT",
    "content-type": "application/json;charset=utf-8",
    "content-length": "263",
    "connection": "keep-alive",
    "keep-alive": "timeout=25",
    "access-control-allow-origin": "*",
    "access-control-expose-headers": "*",
    "x-acs-request-id": "97B1D7B6-F2F6-3A50-97BC-A90B43EC962F",
    "x-acs-trace-id": "29c11fe4c778b74774d5f5602f0e7975",
    "etag": "2a+mcDRTDkXqx9VF7b6U57Q3"
  },
  "statusCode": 200,
  "body": {
    "ResponseCode": "OK",
    "NumberDetail": {
    "Region": "Taiwan",
    "Country": "Taiwan, Province of China",
    "Carrier": "FarEasTone"
  },
    "RequestId": "97B1D7B6-F2F6-3A50-97BC-A90B43EC962F",
    "Segments": "1",
    "ResponseDescription": "OK",
    "To": "88691567****",
    "MessageId": "191921698133637273"
  }
}

Best practices

Performance optimization

  • Reuse HTTP connections with a connection pool to reduce overhead.

  • Use asynchronous calls for high-frequency scenarios.

  • Set timeouts to avoid blocking your service.

  • Use the built-in batch processing for bulk operations.

Security recommendations

  • Always use HTTPS to communicate with the API.

  • Do not log sensitive data such as mobile numbers or verification codes.

  • Validate user-entered mobile numbers to prevent malicious input.

  • Prefer temporary credentials from Alibaba Cloud Security Token Service (STS) over long-term credentials.

Resource management

  • Close unused client connections.

  • Size your thread pool to match expected concurrency.

  • Monitor API call frequency to avoid exceeding QPS limits.

References