All Products
Search
Document Center

Short Message Service:Use SMS SDK for Java

Last Updated:Apr 26, 2024

This topic describes how to call the SendMessageToGlobe API operation to send a message to a mobile number.

Step 1: View the API documentation

Before you call the API operation, we recommend that you learn about the related parameters and permissions. For more information, see SendMessageToGlobe and List of operations by function.

Step 2: Create a RAM user and grant permissions to the RAM user

Important

An Alibaba Cloud account has access permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.

You can call the operation by using an Alibaba Cloud account, a RAM user, or a RAM role. For more information about the differences between the identities, see the "Identity, credential, and authorization" section of the Overview topic.

In this topic, a RAM user is used to call the API operation.

  1. Create a RAM user.

    1. Log on to the Resource Access Management (RAM) console.

    2. In the left-side navigation pane, choose Identities > Users.

    3. On the Users page, click Create User.

    4. On the Create User page, specify the Logon Name and Display Name parameters, and set the Access Mode parameter to Console Access.

    5. Click OK.

      After you create a RAM user, record the logon name and password of the RAM user. When you call API operations, you must use the RAM user to log on to OpenAPI Explorer.

  2. Grant permissions to the RAM user.

    Note

    AliyunDysmsFullAccess: grants the management permissions on Alibaba Cloud SMS.

    AliyunDysmsReadOnlyAccess: grants the read-only permissions on Alibaba Cloud SMS.

    For more information about how to create a custom policy, see RAM authorization.

    1. Access the RAM user list.

    2. On the Users page, find the RAM user and click Add Permissions in the Actions column.

    3. Enter dysms in the search box and select the AliyunDysmsFullAccess policy.

    4. Click OK to complete the authorization.

  3. Go to the details page of the RAM user. On the Authentication tab, click Create AccessKey.

    For more information, see Create an AccessKey pair.

Step 3: Call the operation

In this topic, SMS SDK for Java is used to call the API operation. You can also use SMS SDK for another language. For more information, visit SDK Center. You can use other methods to call the API operation based on your business requirements. For more information, see Overview.

Configure environment variables

This section describes how to configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your operating system.

  • If you use a Linux or macOS operating system, replace the <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> fields in the following commands with your AccessKey ID and AccessKey secret, and then run the commands on your terminal:

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>
  • If you use a Windows operating system, create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, set the variables to your AccessKey ID and AccessKey secret, and then restart the system.

Download the sample code

  1. Visit OpenAPI Explorer.

  2. On the Parameters tab on the left side of the page, configure the request parameters.

    • Sample value of the To parameter: 88691567****

    • Sample value of the Message parameter: This is a test message..

  3. On the SDK Sample Code tab of the page, select V2.0 for SDK Version and Java for Languages. Then, click Download Project to download the sample code package.

  4. Decompress the package to a local path and go to the alibabacloud_sample directory.

Load the project

Use IntelliJ IDEA to load the project. After the relevant dependencies are loaded, open the src/main/java/com/aliyun/sample/Sample.java file. Add the code that is used to print the returned values based on the comments in the code.

Run the project

In the upper-right corner, click Run to view the output logs. 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"
  }
}