コード例 #1
0
        public void StartMns(string _topicName)
        {
            try
            {
                IMNS client = new Aliyun.MNS.MNSClient(_accessKeyId, _secretAccessKey, _endpoint);

                var nativeQueue           = client.GetNativeQueue(_topicName);
                var receiveMessageRequest = new ReceiveMessageRequest();
                receiveMessageRequest.WaitSeconds = 30;

                while (isRunning)
                {
                    try
                    {
                        nativeQueue.BeginReceiveMessage(receiveMessageRequest, ReceiveMessageCallback, nativeQueue);
                        _autoSetEvent.WaitOne();
                    }
                    catch (MNSException me)
                    {
                        throw new ArgumentException("1.接受MNS数据:" + me.Message + me.StackTrace);
                    }
                }
            }
            catch (Exception err)
            {
                throw new ArgumentException("接受MNS数据:" + err.Message + err.StackTrace);
            }
        }
コード例 #2
0
        public async Task SendVerificationCode(string phoneNumber, string code)
        {
            await Task.FromResult(0);

            /**
             * Step 1. 初始化Client
             */
            IMNS client = new Aliyun.MNS.MNSClient(
                SettingManager.GetSettingValue(AppSettingNames.SMS.Ali.AccessKeyId),
                SettingManager.GetSettingValue(AppSettingNames.SMS.Ali.SecretAccessKey),
                SettingManager.GetSettingValue(AppSettingNames.SMS.Ali.RegionEndpoint)
                );

            /**
             * Step 2. 获取主题引用
             */
            Topic topic = client.GetNativeTopic(SettingManager.GetSettingValue(AppSettingNames.SMS.Ali.TopicName));

            /**
             * Step 3. 生成SMS消息属性
             */
            MessageAttributes messageAttributes = new MessageAttributes();
            SmsAttributes     smsAttributes     = new SmsAttributes();

            // 3.1 设置发送短信的签名:SMSSignName
            smsAttributes.FreeSignName = SettingManager.GetSettingValue(AppSettingNames.SMS.FreeSignName);
            // 3.2 设置发送短信的模板SMSTemplateCode
            smsAttributes.TemplateCode = SettingManager.GetSettingValue(AppSettingNames.SMS.Ali.TemplateCode);
            Dictionary <string, string> param = new Dictionary <string, string>();

            // 3.3 (如果短信模板中定义了参数)设置短信模板中的参数,发送短信时,会进行替换
            param.Add("name", code);
            // 3.4 设置短信接收者手机号码
            smsAttributes.Receiver          = phoneNumber;
            smsAttributes.SmsParams         = param;
            messageAttributes.SmsAttributes = smsAttributes;
            PublishMessageRequest request = new PublishMessageRequest();

            request.MessageAttributes = messageAttributes;

            /**
             * Step 4. 设置SMS消息体(必须)
             *
             * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
             */
            request.MessageBody = "smsmessage";
            try
            {
                /**
                 * Step 5. 发布SMS消息
                 */
                PublishMessageResponse resp = topic.PublishMessage(request);
            }
            catch (Exception e)
            {
                Logger.Debug("发送短信失败", e);
            }
        }
コード例 #3
0
        /// <summary>
        /// 身份验证验证码
        /// </summary>
        /// <param name="recNum">接收号码</param>
        /// <returns>6位验证码</returns>
        public static string AuthenticationVerifyCode(string recNum)
        {
            Random ran  = new Random();
            string code = ran.RandomNumber(6, true);

            /**
             * Step 1. 初始化Client
             */
            IMNS client = new Aliyun.MNS.MNSClient(Permanent.ACCESS_KEY_ID, Permanent.ACCESS_KEY_SECRET, Permanent.ENDPOINT);

            /**
             * Step 2. 获取主题引用
             */
            Topic topic = client.GetNativeTopic(Permanent.TOPIC_NAME);

            /**
             * Step 3. 生成SMS消息属性
             */
            MessageAttributes  messageAttributes  = new MessageAttributes();
            BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();

            // 3.1 设置发送短信的签名:SMSSignName
            batchSmsAttributes.FreeSignName = "幻幻平台";
            // 3.2 设置发送短信的模板SMSTemplateCode
            batchSmsAttributes.TemplateCode = "SMS_25255320";
            Dictionary <string, string> param = new Dictionary <string, string>();

            // 3.3 (如果短信模板中定义了参数)设置短信模板中的参数,发送短信时,会进行替换
            param.Add("code", code);
            param.Add("product", "幻幻社区");
            // 3.4 设置短信接收者手机号码
            batchSmsAttributes.AddReceiver(recNum, param);

            messageAttributes.BatchSmsAttributes = batchSmsAttributes;
            PublishMessageRequest request = new PublishMessageRequest();

            request.MessageAttributes = messageAttributes;

            /**
             * Step 4. 设置SMS消息体(必须)
             *
             * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
             */
            request.MessageBody = "smsmessage";

            /**
             * Step 5. 发布SMS消息
             */
            PublishMessageResponse resp = topic.PublishMessage(request);

            //return resp.MessageId;
            return(code);
        }
コード例 #4
0
        public void SetAccountAttributesTest()
        {
            IMNS client = new Aliyun.MNS.MNSClient(_accessKeyId, _secretAccessKey, _endpoint);

            var resp = client.GetAccountAttributes();
            var originalLoggingBucket = resp.Attributes.LoggingBucket;

            AccountAttributes aa1 = new AccountAttributes();

            client.SetAccountAttributes(aa1);
            resp = client.GetAccountAttributes();
            Assert.AreEqual(originalLoggingBucket, resp.Attributes.LoggingBucket);

            AccountAttributes aa2 = new AccountAttributes()
            {
                LoggingBucket = "Test"
            };

            client.SetAccountAttributes(aa2);
            resp = client.GetAccountAttributes();
            Assert.AreEqual("Test", resp.Attributes.LoggingBucket);

            AccountAttributes aa3 = new AccountAttributes();

            client.SetAccountAttributes(aa3);
            resp = client.GetAccountAttributes();
            Assert.AreEqual("Test", resp.Attributes.LoggingBucket);

            AccountAttributes aa4 = new AccountAttributes()
            {
                LoggingBucket = "Test"
            };

            client.SetAccountAttributes(aa4);
            resp = client.GetAccountAttributes();
            Assert.AreEqual("Test", resp.Attributes.LoggingBucket);

            AccountAttributes aa5 = new AccountAttributes()
            {
                LoggingBucket = ""
            };

            client.SetAccountAttributes(aa5);
            resp = client.GetAccountAttributes();
            Assert.AreEqual("", resp.Attributes.LoggingBucket);
        }
コード例 #5
0
        public static Boolean sendSmsViaMnsSDK(String signName, String accessKey, String accessSecret, String smsTemplate, String recNum, String code, String endpoint, String topicName)
        {
            try {
                IMNS client = new Aliyun.MNS.MNSClient(accessKey, accessSecret, endpoint);

                /**
                 * Step 2. 获取主题引用
                 */
                Topic topic = client.GetNativeTopic(topicName);

                /**
                 * Step 3. 生成SMS消息属性
                 */
                MessageAttributes  messageAttributes  = new MessageAttributes();
                BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();
                // 3.1 设置发送短信的签名:SMSSignName
                batchSmsAttributes.FreeSignName = signName;
                // 3.2 设置发送短信的模板SMSTemplateCode
                batchSmsAttributes.TemplateCode = smsTemplate;
                Dictionary <string, string> param = new Dictionary <string, string>();
                // 3.3 (如果短信模板中定义了参数)设置短信模板中的参数,发送短信时,会进行替换
                param.Add("code", code);
                // 3.4 设置短信接收者手机号码
                batchSmsAttributes.AddReceiver(recNum, param);
                //batchSmsAttributes.AddReceiver("$YourReceiverPhoneNumber2", param);
                messageAttributes.BatchSmsAttributes = batchSmsAttributes;
                PublishMessageRequest request = new PublishMessageRequest();
                request.MessageAttributes = messageAttributes;

                /**
                 * Step 4. 设置SMS消息体(必须)
                 *
                 * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
                 */
                request.MessageBody = "smsmessage";
                PublishMessageResponse resp = topic.PublishMessage(request);
                return(true);
            }
            catch (Exception e) {
                return(false);
            }
        }
コード例 #6
0
        public void Register(string code, string telephone)
        {
            IMNS               client             = new Aliyun.MNS.MNSClient(_accessKeyId, _secretAccessKey, _reginEndPoint);
            Topic              topic              = client.GetNativeTopic(_topicName);
            MessageAttributes  messageAttributes  = new MessageAttributes();
            BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();

            // 3.1 设置发送短信的签名:SMSSignName
            batchSmsAttributes.FreeSignName = _freeSignName;
            // 3.2 设置发送短信的模板SMSTemplateCode
            batchSmsAttributes.TemplateCode = "SMS_62610202";
            Dictionary <string, string> param = new Dictionary <string, string>();

            // 3.3 (如果短信模板中定义了参数)设置短信模板中的参数,发送短信时,会进行替换
            param.Add("code", code);
            param.Add("product", _freeSignName);

            // 3.4 设置短信接收者手机号码
            batchSmsAttributes.AddReceiver(telephone, param);

            messageAttributes.BatchSmsAttributes = batchSmsAttributes;
            PublishMessageRequest request = new PublishMessageRequest();

            request.MessageAttributes = messageAttributes;

            /**
             * Step 4. 设置SMS消息体(必须)
             *
             * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
             */
            request.MessageBody = "smsmessage";
            try
            {
                PublishMessageResponse resp = topic.PublishMessage(request);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Publish SMS message failed, exception info: " + ex.Message);
            }
        }
コード例 #7
0
ファイル: ReceiveMsg.cs プロジェクト: cqiejp/dysms_net
 /// 处理消息
 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 = Convert.ToDateTime(DateTime.Now);
                     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("Hello World!", e);
                 }
             }
         }
         catch (Exception e)
         {
             System.Console.WriteLine("Handle exception", e);
         }
         Thread.Sleep(sleepTime);
     }
 }
コード例 #8
0
        /// 管理控制台中配置的短信签名(状态必须是验证通过)
        //private const string SignName = "深圳市利德行";
        //private const string SignName = "一生时光";

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public static bool SendSMS(SendSmsRequest req)
        {
            var flag = true;

            /**
             * Step 1. 初始化Client
             */
            IMNS client = new Aliyun.MNS.MNSClient(_accessKeyId, _secretAccessKey, _endpoint);

            /**
             * Step 2. 获取主题引用
             */
            Topic topic = client.GetNativeTopic(_topicName);

            /**
             * Step 3. 生成SMS消息属性
             */
            MessageAttributes  messageAttributes  = new MessageAttributes();
            BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();

            // 3.1 设置发送短信的签名:SMSSignName
            batchSmsAttributes.FreeSignName = req.SignName;
            // 3.2 设置发送短信的模板SMSTemplateCode
            //batchSmsAttributes.TemplateCode = "SMS_61750032";
            batchSmsAttributes.TemplateCode = req.TemplateCode;
            //Dictionary<string, string> param = new Dictionary<string, string>();
            // 3.3 (如果短信模板中定义了参数)设置短信模板中的参数,发送短信时,会进行替换

            //param.Add("code", "4567");
            // 3.4 设置短信接收者手机号码


            if (!string.IsNullOrWhiteSpace(req.RecNum))
            {
                batchSmsAttributes.AddReceiver(req.RecNum, req.ParamString);
            }
            else
            {
                ///循环电话号码
                foreach (var item in req.LiRecNum)
                {
                    batchSmsAttributes.AddReceiver(item, req.ParamString);
                }
            }



            messageAttributes.BatchSmsAttributes = batchSmsAttributes;
            PublishMessageRequest request = new PublishMessageRequest();

            request.MessageAttributes = messageAttributes;

            /**
             * Step 4. 设置SMS消息体(必须)
             *
             * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
             */
            request.MessageBody = "smsmessage";
            try
            {
                /**
                 * Step 5. 发布SMS消息
                 */
                PublishMessageResponse resp = topic.PublishMessage(request);
                //Console.WriteLine(resp.MessageId);
            }
            catch (Exception ex)
            {
                //Console.WriteLine("Publish SMS message failed, exception info: " + ex.Message);
                //Logger.Debug("错误信息:"+ex.Message + "!接收号码:" + req.RecNum + "!短信码:" + req.ParamString["code"] + "!签名:" + req.SignName + "!模板" + req.TemplateCode);
                flag = false;
            }

            return(flag);
        }