All Products
Search
Document Center

C#

Last Updated: Nov 26, 2019

Input parameters

Name Type O/M Example Description
MessageType String Mandatory SmsReport SmsReport
QueueName String Mandatory Alicom-Queue-xxxxxx-SmsReport The name of the message queue returned from the console——System Setting.

Output parameters

Name Name Example Description
MessageId String 10080303003003 The ID generated by Alibaba Cloud to uniquely identify an SMS message.
To String 452220121 The phone number of the SMS message recipient.
Status Number 3 The status of the SMS message. Valid values:1 (Delivered), 2 (Failed), and 3 (Submitted).
ErrorCode String success The returned error code.
ErrorDescription String success The returned error message.
SendDate String Mon, 24 Dec 2018 16:58:22 +0800 The time and date when the SMS message was sent.
ReceiveDate String Mon, 24 Dec 2018 16:58:22 +0800 The time and date when the SMS message was received.

Developer integration process

1.SDK download: Click to view the download link

2.Writing a sample program

When calling the interface, please note:

  • The regionIdForPop must be ”ap-southeast-1“, please do not modify it.
  • domainForPop must be ”dybaseapi.ap-southeast-1.aliyuncs.com“, please do not modify it.
  • productName must be ”Dybaseapi”, please do not modify it.
  • mnsAccountEndpoint must be “http://1493622401794734.mns.ap-southeast-1.aliyuncs.com”, please do not modify.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aliyun.Acs.Core;
  6. using Aliyun.Acs.Core.Exceptions;
  7. using Aliyun.Acs.Core.Profile;
  8. using Aliyun.Acs.Dybaseapi.Model.V20180501;
  9. using Aliyun.MNS;
  10. using Aliyun.MNS.Model;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace ConsoleApplication2
  14. {
  15. class Program
  16. {
  17. //Set the size of asynchronous thread
  18. private static int maxThread = 2;
  19. static String accessId = "yourAccessId";
  20. static String accessSecret = "yourAccessSecret";
  21. //Message Type, there is only two type: smsreport, upmessage
  22. static String messageType = "SmsReport";
  23. //Message Quenue Name, you can get it from SMS console, like:Alicom-Queue-xxxxxx-SmsReport
  24. static String queueName = "Alicom-Queue-xxxxxxxxxxxx-SmsReport";
  25. static String domainForPop = "sms-intl.ap-southeast-1.aliyuncs.com";
  26. static String regionIdForPop = "ap-southeast-1";
  27. static String productName = "Dybaseapi";
  28. static IAcsClient acsClient = null;
  29. public static IAcsClient InitAcsClient(String regionIdForPop, String accessId, String accessSecret, String productName, String domainForPop)
  30. {
  31. IClientProfile profile = DefaultProfile.GetProfile(regionIdForPop, accessId, accessSecret);
  32. DefaultProfile.AddEndpoint(regionIdForPop, regionIdForPop, productName, domainForPop);
  33. IAcsClient acsClient = new DefaultAcsClient(profile);
  34. return acsClient;
  35. }
  36. private static void InitData()
  37. {
  38. acsClient = InitAcsClient(regionIdForPop, accessId, accessSecret, productName, domainForPop);
  39. }
  40. static void Main(string[] args)
  41. {
  42. InitData();
  43. for (int i = 0; i < maxThread; i++)
  44. {
  45. TestTask testTask = new TestTask("PullMessageTask-thread-" + i, messageType, queueName, acsClient);
  46. Thread t = new Thread(new ThreadStart(testTask.Handle));
  47. t.Start();
  48. }
  49. Console.ReadKey();
  50. }
  51. }
  52. class TestTask
  53. {
  54. private object o = new object();
  55. private int sleepTime = 50;
  56. public String name { get; private set; }
  57. public String messageType { get; private set; }
  58. public String queueName { get; private set; }
  59. public int TaskID { get; private set; }
  60. public IAcsClient acsClient { get; private set; }
  61. public TestTask(String name, String messageType, String queueName, IAcsClient acsClient)
  62. {
  63. this.name = name;
  64. this.messageType = messageType;
  65. this.queueName = queueName;
  66. this.acsClient = acsClient;
  67. }
  68. long bufferTime = 60 * 2;
  69. String mnsAccountEndpoint = "https://1493622401794734.mns.ap-southeast-1.aliyuncs.com/";
  70. Dictionary<string, QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO> tokenMap = new Dictionary<string, QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO>();
  71. Dictionary<string, Queue> queueMap = new Dictionary<string, Queue>();
  72. public QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO GetTokenByMessageType(IAcsClient acsClient, String messageType)
  73. {
  74. QueryTokenForMnsQueueRequest request = new QueryTokenForMnsQueueRequest();
  75. request.MessageType = messageType;
  76. QueryTokenForMnsQueueResponse queryTokenForMnsQueueResponse = acsClient.GetAcsResponse(request);
  77. QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO token = queryTokenForMnsQueueResponse.MessageTokenDTO;
  78. return token;
  79. }
  80. public void Handle()
  81. {
  82. while (true)
  83. {
  84. try
  85. {
  86. QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO token = null;
  87. Queue queue = null;
  88. lock (o)
  89. {
  90. if (tokenMap.ContainsKey(messageType))
  91. {
  92. token = tokenMap[messageType];
  93. }
  94. if (queueMap.ContainsKey(queueName))
  95. {
  96. queue = queueMap[queueName];
  97. }
  98. TimeSpan ts = new TimeSpan(0);
  99. if (token != null)
  100. {
  101. DateTime b = Convert.ToDateTime(token.ExpireTime);
  102. DateTime c = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
  103. ts = b - c;
  104. }
  105. if (token == null || ts.TotalSeconds < bufferTime || queue == null)
  106. {
  107. token = GetTokenByMessageType(acsClient, messageType);
  108. IMNS client = new Aliyun.MNS.MNSClient(token.AccessKeyId, token.AccessKeySecret, mnsAccountEndpoint, token.SecurityToken);
  109. queue = client.GetNativeQueue(queueName);
  110. if (tokenMap.ContainsKey(messageType))
  111. {
  112. tokenMap.Remove(messageType);
  113. }
  114. if (queueMap.ContainsKey(queueName))
  115. {
  116. queueMap.Remove(queueName);
  117. }
  118. tokenMap.Add(messageType, token);
  119. queueMap.Add(queueName, queue);
  120. }
  121. }
  122. BatchReceiveMessageResponse batchReceiveMessageResponse = queue.BatchReceiveMessage(16);
  123. List<Message> messages = batchReceiveMessageResponse.Messages;
  124. for (int i = 0; i <= messages.Count - 1; i++)
  125. {
  126. try
  127. {
  128. byte[] outputb = Convert.FromBase64String(messages[i].Body);
  129. string orgStr = Encoding.UTF8.GetString(outputb);
  130. System.Console.WriteLine(orgStr);
  131. //TODO
  132. queue.DeleteMessage(messages[i].ReceiptHandle);
  133. }
  134. catch (Exception e)
  135. {
  136. System.Console.WriteLine("Message Body exception", e);
  137. }
  138. }
  139. }
  140. catch (Exception e)
  141. {
  142. System.Console.WriteLine("Handle exception", e);
  143. }
  144. Thread.Sleep(sleepTime);
  145. }
  146. }
  147. }
  148. }