You can receive SMS verification code delivery status reports (DypnsSmsVerifyReport) through MNS message queues. Configure receipt reception for the SendSmsVerifyCode API.
Prerequisites
-
Register an Alibaba Cloud account and generate an AccessKey pair. For more information, see Create an AccessKey for a RAM user.
-
Ensure you can ping the following endpoints from your local machine:
dysmsapi.aliyuncs.com,mns.cn-hangzhou.aliyuncs.com, ordybaseapi.aliyuncs.com. -
Create a Resource Access Management (RAM) user and grant the RAM user permissions for Phone Number Verification Service and Message Service (MNS).
-
Read Introduction to delivery receipts and configuration process to understand receipt message patterns, types, and setup steps.
DypnsSmsVerifyReport message body format
|
Name |
Type |
Example |
Description |
|
send_time |
String |
2021-09-06 15:49:55 |
Time when the message was forwarded to the carrier. |
|
report_time |
String |
2021-09-06 15:49:58 |
Time when the delivery receipt was received from the carrier. |
|
success |
Boolean |
true |
Indicates whether the message was sent successfully. Valid values:
|
|
err_msg |
String |
User received successfully |
Description of the error code. |
|
err_code |
String |
DELIVERED |
Error code. |
|
phone_number |
String |
159****6532 |
Recipient phone number. |
|
sms_size |
String |
1 |
SMS length. For details about how SMS length is calculated, see SMS sending rules. |
|
biz_id |
String |
12345 |
Delivery receipt ID, also known as the transaction serial number. Each call to the SendSmsVerifyCode API under the same Alibaba Cloud account returns a unique BizId. |
|
out_id |
String |
123456 |
The |
Download demo
See Message Service (formerly MNS) consumption demo. Download the demo and SDK for your preferred programming language. This topic uses Java as an example for subsequent steps.
After downloading the demo, import the JAR packages from the
libdirectory by clickingAdd as Library, as shown in the figure below.You can also find the Maven dependency in the
pom.xmlfile and install the Alibaba Cloud SDK for Java.

Parameter settings
Before using this example, configure the following parameters.
Set AccessKey
Set message type
Set queue name
Complete example
The dealMessage method processes the received status report. Add your business logic for handling upstream message content inside this method.
// Parse the message body according to the specific message format described in this document
String arg = (String) contentMap.get("arg");
// Add your business code here
The arg variable represents a parameter from the receipt message body. Valid values include the following: send_time, report_time, success, err_msg, err_code, phone_number, sms_size, biz_id, and out_id.
package com.alicom.mns.sample;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.alicom.mns.tools.DefaultAlicomMessagePuller;
import com.alicom.mns.tools.MessageListener;
import com.aliyun.mns.model.Message;
import com.google.gson.Gson;
/**
* Use only for receiving messages from Cloud Communication.
* Do not use for other services.
*/
public class ReceiveDemo {
private static Log logger=LogFactory.getLog(ReceiveDemo.class);
static class MyMessageListener implements MessageListener{
private Gson gson=new Gson();
@Override
public boolean dealMessage(Message message) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Key message attributes
System.out.println("message receiver time from mns:" + format.format(new Date()));
System.out.println("message handle: " + message.getReceiptHandle());
System.out.println("message body: " + message.getMessageBodyAsString());
System.out.println("message id: " + message.getMessageId());
System.out.println("message dequeue count:" + message.getDequeueCount());
System.out.println("Thread:" + Thread.currentThread().getName());
try{
Map<String,Object> contentMap=gson.fromJson(message.getMessageBodyAsString(), HashMap.class);
// Parse the message body according to the specific message format described in this document
String arg = (String) contentMap.get("arg");
// Add your business code here
}catch(com.google.gson.JsonSyntaxException e){
logger.error("error_json_format:"+message.getMessageBodyAsString(),e);
// Message format errors should not occur. If they do, delete the message to prevent repeated failures.
return true;
} catch (Throwable e) {
// Return false for exceptions caused by your own code so the message is not deleted and will be retried based on the retry policy.
return false;
}
// Return true to indicate successful processing. The SDK will then call MNS's delete method to remove the message from the queue.
return true;
}
}
public static void main(String[] args) throws Exception, ParseException {
DefaultAlicomMessagePuller puller=new DefaultAlicomMessagePuller();
// Set thread pool size, task queue size, and idle thread sleep time
puller.setConsumeMinThreadSize(6);
puller.setConsumeMaxThreadSize(16);
puller.setThreadQueueSize(200);
puller.setPullMsgThreadSize(1);
// Enable debug logging only during server-side debugging. Disable it otherwise to avoid performance overhead.
puller.openDebugLog(false);
// Retrieve AccessKey ID and AccessKey secret from environment variables
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
/*
* Replace messageType and queueName with your actual message type and queue name.
* Supported Cloud Communication receipt message types:
* 1: SMS delivery receipt: SmsReport
* 2: SMS upstream: SmsUp
* 3: International SMS delivery receipt: GlobeSmsReport
*/
String messageType="<MESSAGE_TYPE>"; // Replace with your product's message type
String queueName="<QUEUE_NAME>"; // After enabling the corresponding service in the Cloud Communication console, you can find the queue name there (format: Alicom-Queue-******-SmsUp)
puller.startReceiveMsg(accessKeyId,accessKeySecret,messageType,queueName,new MyMessageListener());
}
}
Sample output after running the demo:
/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/bin/java ...
Connected to the target VM, address: '127.0.0.1:62393', transport: 'socket'
14:00:07.056 [PullMessageTask-thread-0] DEBUG com.aliyun.mns.client.CloudAccount - initiated CloudAccount, accessId: xxx accessKey=xxx
message receiver time from mns: xxx
message handle: xxx
message body: xxx
message id: xxx
message dequeue count:1
Thread:pool-1-thread-1