×
Community Blog Sending Text Messages with Alibaba Cloud SMS

Sending Text Messages with Alibaba Cloud SMS

In this tutorial, we'll show you how you can send messages all around the world with Alibaba Cloud Short Message Service (SMS).

By Kenny Lai, Solutions Architect

Alibaba Cloud Short Message Service (SMS) is a messaging service that covers the globe, providing convenient, efficient, and intelligent communication capabilities that help businesses quickly contact their customers. You can call API operations to send verification codes, notifications, and marketing messages. SMS delivered 600 million messages to 200 million users during the Alibaba 11/11 Global Shopping Festival.

Prerequisites

  1. An enterprise Alibaba Cloud account
  2. A RAM account with AliyunDysmFullAccess policy attached
  3. A mobile phone number which can receive SMS
  4. Some programming skill (java in this example)

Steps

Check if your Alibaba Cloud account is an enterprise account. If you have previously registered for an individual account, you can file a work order to request for an upgrade.

1

Locate SMS console on the Alibaba Cloud console.

2

From the SMS console overview page, navigate to Go Globe.

3

Click New Content

Content Type : OTP

Subject : SMS OTP

SMS Content: Your Verification Code: ${code}, will expire 5 minutes later.

Click Submit and a new entry will appear.

4

Record the Content Code as it will be reference in the program later.

Use the sample code below

Remember to update the variables as follows:

  • accessKeyId: RAM account's access key
  • accessKeySecret: RAM account's Secret
  • tel: Telephone number you would send the SMS to

Sample Code

package com.alicom.dysms.api;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20180501.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20180501.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20180501.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20180501.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * Created on 2018.05.01
 *
 * The is a DEMO that introduces how to use SMS send Api and SMS query Api.
 * Two jar packages are required,please put them in the libs directory
 * 1:aliyun-java-sdk-core.jar
 * 2:aliyun-java-sdk-dysmsapi-inter.jar
 *
 *  notice : The Program was encoded with standard of UTF-8
 *
 */
public class SmsDemo {

    // product name, please remain unchanged
    static final String product = "Dysmsapi";
    // product domain, please remain unchanged
    static final String domain = "dysmsapi.ap-southeast-1.aliyuncs.com";

    // AccessKey and AccessKeySecret , you can login sms console and find it in API Management
    static final String accessKeyId = "use your Key id";
    static final String accessKeySecret = "use your Key Secret";

    static final String tel = "yourphonenumber with IDD prefix";
    public static SendSmsResponse sendSms() throws ClientException {

        try {
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

            IClientProfile profile = DefaultProfile.getProfile("ap-southeast-1", accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("ap-southeast-1", "ap-southeast-1", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            // initiate the SendSmsRequest, read help documents for more parameters instructions
            SendSmsRequest request = new SendSmsRequest();
            // send to
            request.setPhoneNumbers(tel);
            // ContentCode , you can login sms console and find it in Content Management
            request.setContentCode("SMS_10350063"); // this should match the content code shows in sms console
            // set the value for parameters in sms Content with JSON format. For example, the content is "Your Verification Code : ${code}, will be expired 5 minutes later"
            request.setContentParam("{\"code\":\"1234\"}");
            // Optional,custom field, this value will be returned in the sms delivery report.
            request.setExternalId("E0012033");

            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

            return sendSmsResponse;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    public static void main(String[] args) throws ClientException, InterruptedException {

        // send sms
        SendSmsResponse response = sendSms();
        System.out.println("send sms response----------------");
        System.out.println("Code=" + response.getResultCode());
        System.out.println("Message=" + response.getResultMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());

        Thread.sleep(3000L);
        //done
   
    }
}

Corresponding maven pom file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.alicom.dysms</groupId>
    <artifactId>alicom-dysms-api</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.dysms</groupId>
            <artifactId>alicom-dysms-api</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/aliyun-java-sdk-dysmsapi-inter-1.0.0.jar</systemPath>
        </dependency>
    </dependencies>
</project>

And the dysms jar package can be downloaded from link https://www.alibabacloud.com/help/doc-detail/73676.htm

You should receive an SMS as shown in the image below.

5

From the console, you can check the SMS delivery info.

6

The SMS console also supports checking the delivery status (you can also get the info using API).

7

That's it. Enjoy using Alibaba Cloud Short Message Service (SMS) to global.

1 1 1
Share on

Alibaba Clouder

1,450 posts | 232 followers

You may also like

Comments

Raja_KT February 14, 2019 at 6:34 am

The response is the one we get back into the application, calling for authentication or verification... right?

Alibaba Clouder

1,450 posts | 232 followers

Related Products

  • Short Message Service

    A messaging service that covers the globe, Alibaba SMS provides convenient, efficient, and intelligent communication capabilities that help businesses quickly contact their customers.

    Learn More
  • DirectMail

    A simple and efficient email service to help you send transactional notifications and batch email

    Learn More
  • Message Service

    A message queuing and notification service that facilitates smooth transfer of messages between applications Get it Free Contact Sales

    Learn More