本文为您介绍了C#调用SmsReport查看短信回执。

请求参数

名称 类型 是否必选 示例值 描述
MessageType String SmsReport 有一种消息类型:SmsReport。
QueueName String Alicom-Queue-xxxxxx-SmsReport 阿里巴巴提供的消息队列名称,在短信控制台--系统设置可以查看到。

返回数据

名称 类型 示例值 描述
MessageId String 100803***03003 短信的发送ID。
To String 45***0121 短信的发送号码。
Status Number 3 短信的发送状态: 1 (Submitted), 2 (Failed), and 3 (Delivered)。
ErrorCode String success 短信发送返回的错误码。
ErrorDescription String success 短信发送返回的错误码详细描述。
SendDate String Mon, 24 Dec 2018 16:58:22 +0800 短信转发给运营商的时间。
ReceiveDate String Mon, 24 Dec 2018 16:58:22 +0800 短信收到运营商回执的时间。
NumberDetail String 号码的详细属性。
Country String Hongkong, China 号码所属国家。
Region String HongKong Number Region。
Carrier String CMI 号码所属的运营商网络。

引入阿里云核心包

SDK下载:点击查看下载链接

调用示例

注意事项如下:

  • regionIdForPop必须为ap-southeast-1,请勿修改。
  • domainForPop必须为dysmsapi.ap-southeast-1.aliyuncs.com,请勿修改。
  • productName必须为Dysmsapi,请勿修改。
  • mnsAccountEndpoint必须为http://1493622401794734.mns.ap-southeast-1.aliyuncs.com, 请勿修改。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;

using Aliyun.Acs.Dybaseapi.Model.V20180501;
using Aliyun.MNS;
using Aliyun.MNS.Model;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication2
{


    class Program
    {
        //Set the size of asynchronous thread
        private static int maxThread = 2;
        static String accessId = "yourAccessId";
        static String accessSecret = "yourAccessSecret";
        //Message Type, there is only two type: smsreport, upmessage
        static String messageType = "SmsReport";
        //Message Quenue Name, you can get it from SMS console, like:Alicom-Queue-xxxxxx-SmsReport
        static String queueName = "Alicom-Queue-xxxxxxxxxxxx-SmsReport";
        static String domainForPop = "dysmsapi.ap-southeast-1.aliyuncs.com";
        static String regionIdForPop = "ap-southeast-1";
        static String productName = "Dysmsapi";
        static IAcsClient acsClient = null;
        public static IAcsClient InitAcsClient(String regionIdForPop, String accessId, String accessSecret, String productName, String domainForPop)
        {
            IClientProfile profile = DefaultProfile.GetProfile(regionIdForPop, accessId, accessSecret);
            DefaultProfile.AddEndpoint(regionIdForPop, regionIdForPop, productName, domainForPop);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            return acsClient;
        }

        private static void InitData()
        {
            acsClient = InitAcsClient(regionIdForPop, accessId, accessSecret, productName, domainForPop);
        }
        static void Main(string[] args)
        {
            InitData();
            for (int i = 0; i < maxThread; i++)
            {
                TestTask testTask = new TestTask("PullMessageTask-thread-" + i, messageType, queueName, acsClient);
                Thread t = new Thread(new ThreadStart(testTask.Handle));

                t.Start();
            }
            Console.ReadKey();
        }
    }
    class TestTask
    {
        private object o = new object();
        private int sleepTime = 50;
        public String name { get; private set; }
        public String messageType { get; private set; }
        public String queueName { get; private set; }
        public int TaskID { get; private set; }
        public IAcsClient acsClient { get; private set; }
        public TestTask(String name, String messageType, String queueName, IAcsClient acsClient)
        {
            this.name = name;
            this.messageType = messageType;
            this.queueName = queueName;
            this.acsClient = acsClient;
        }
        long bufferTime = 60 * 2;
        String mnsAccountEndpoint = "https://1493622401794734.mns.ap-southeast-1.aliyuncs.com/";
        Dictionary<string, QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO> tokenMap = new Dictionary<string, QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO>();
        Dictionary<string, Queue> queueMap = new Dictionary<string, Queue>();
        public QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO GetTokenByMessageType(IAcsClient acsClient, String messageType)
        {
            QueryTokenForMnsQueueRequest request = new QueryTokenForMnsQueueRequest();
            request.MessageType = messageType;
            QueryTokenForMnsQueueResponse queryTokenForMnsQueueResponse = acsClient.GetAcsResponse(request);
            QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO token = queryTokenForMnsQueueResponse.MessageTokenDTO;
            return token;
        }

        public void Handle()
        {
            while (true)
            {
                try
                {
                    QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO token = null;
                    Queue queue = null;
                    lock (o)
                    {
                        if (tokenMap.ContainsKey(messageType))
                        {
                            token = tokenMap[messageType];
                        }
                        if (queueMap.ContainsKey(queueName))
                        {
                            queue = queueMap[queueName];
                        }
                        TimeSpan ts = new TimeSpan(0);
                        if (token != null)
                        {
                            DateTime b = Convert.ToDateTime(token.ExpireTime);
                            DateTime c = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
                            ts = b - c;
                        }
                        if (token == null || ts.TotalSeconds < bufferTime || queue == null)
                        {
                            token = GetTokenByMessageType(acsClient, messageType);
                            IMNS client = new Aliyun.MNS.MNSClient(token.AccessKeyId, token.AccessKeySecret, mnsAccountEndpoint, token.SecurityToken);
                            queue = client.GetNativeQueue(queueName);
                            if (tokenMap.ContainsKey(messageType))
                            {
                                tokenMap.Remove(messageType);
                            }
                            if (queueMap.ContainsKey(queueName))
                            {
                                queueMap.Remove(queueName);
                            }
                            tokenMap.Add(messageType, token);
                            queueMap.Add(queueName, queue);
                        }
                    }
                    BatchReceiveMessageResponse batchReceiveMessageResponse = queue.BatchReceiveMessage(16);
                    List<Message> messages = batchReceiveMessageResponse.Messages;
                    for (int i = 0; i <= messages.Count - 1; i++)
                    {
                        try
                        {
                            byte[] outputb = Convert.FromBase64String(messages[i].Body);
                            string orgStr = Encoding.UTF8.GetString(outputb);
                            System.Console.WriteLine(orgStr);
                            //TODO
                            queue.DeleteMessage(messages[i].ReceiptHandle);
                        }
                        catch (Exception e)
                        {
                            System.Console.WriteLine("Message Body exception", e);
                        }
                    }
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("Handle exception", e);
                }
                Thread.Sleep(sleepTime);
            }
        }


    }
}