Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="regionCode">86:中国大陆, 852:中华人民共和国大陆地区</param>
        /// <param name="smsContent"></param>
        /// <param name="receiverMobiles"></param>
        /// <param name="senderUserId"></param>
        /// <param name="senderSupplierId"></param>
        /// <param name="pushType"></param>
        /// <param name="objectId"></param>
        /// <returns></returns>
        public bool SendSms(string regionCode, string smsContent, List <string> receiverMobiles, int senderUserId, int senderSupplierId, int pushType = 2, long objectId = 0)
        {
            var result = false;

            if (receiverMobiles.Count > 0)
            {
                var messageInfo = new MessageInfo
                {
                    Title              = "",
                    Content            = smsContent,
                    CreateBy           = senderUserId.ToString(),
                    CreateTime         = DateTime.Now,
                    Timing             = Convert.ToDateTime("1900-1-1"),
                    MesasgeTypeContent = string.Join(",", receiverMobiles).TrimEnd(','),
                    MessageType        = 1,
                    SendWay            = SendWay.Auto.GetHashCode(),
                    SendType           = SendType.ByMobile.GetHashCode(),
                    CreateUserType     = 1,
                    Status             = (int)MessageStatus.Wait,
                    Sender             = senderSupplierId,
                    ContentType        = 0,
                    PushType           = pushType,
                    ObjectId           = objectId
                };
                try
                {
                    using (TransactionScope unitOfWork = new TransactionScope())
                    {
                        //创建消息并获取ID
                        messageInfo.ID = messageDal.Insert(messageInfo);
                        string mobileNo = messageInfo.MesasgeTypeContent;
                        if (!string.IsNullOrEmpty(mobileNo))
                        {
                            //开发阶段暂时不发短信
                            string username = "******";
                            string password = "******";
                            string content  = smsContent;
                            //string content = System.Web.HttpUtility.UrlEncode(string.Format("健康绿氧购物商城{0}", smsContent), Encoding.GetEncoding("UTF-8"));

                            string doResult = smsClient.SendMessage(username, password, string.Empty, content, regionCode + mobileNo, string.Empty);
                            if (!string.IsNullOrEmpty(doResult))
                            {
                                //<ReturnValue>
                                //  <State>1</State>
                                //  <Count>2</Count>
                                //  <ResponseID>14979274</ResponseID>
                                //</ReturnValue>

                                XmlDocument doc = new XmlDocument();
                                doc.LoadXml(doResult);
                                string state = doc.FirstChild.ChildNodes[0].InnerText;
                                if (state == "1")
                                {
                                    //短信发送成功,更新消息状态
                                    result = messageDal.UpdateMessageStatus(messageInfo.ID, (int)MessageStatus.Success);
                                }
                            }
                        }
                        unitOfWork.Complete();
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.ErrorMsg(string.Format("发送消息异常:{0}", ex));
                }
            }
            return(result);
        }