码迷,mamicode.com
首页 > 移动开发 > 详细

用“网建”平台发手机短信的C#代码

时间:2018-03-21 13:56:36      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:msm   break   def   throw   namespace   字符   article   xxxxxx   搜索   

一直都用这个平台发手机短信的,今天做新项目的时候用到了,但是上来博客搜索不到,只好翻以前的源代码翻了好久才找到了,先记下来,以作备用:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace Niunan.CardShop.Web.Code
{
    public class SendMobile
    {

        /// <summary>返回发送短信的状态说明
        ///
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMobileMSMStatus(string str)
        {
            string s = "";
            switch (str)
            {
                case "-1":
                    s = "没有该用户账户";
                    break;
                case "-2":
                    s = "密钥不正确(不是用户密码)";
                    break;
                case "-3":
                    s = "短信数量不足";
                    break;
                case "-11":
                    s = "该用户被禁用";
                    break;
                case "-14":
                    s = "短信内容出现非法字符";
                    break;
                case "-4":
                    s = "手机号格式不正确";
                    break;
                case "-41":
                    s = "手机号码为空";
                    break;
                case "-42":
                    s = "短信内容为空";
                    break;
                default:
                    s = "成功发送" + str + "条短信";
                    break;
            }
            return s;
        }
        /// <summary>发送手机短信
        ///
        /// </summary>
        /// <param name="mobile">手机号码,多个手机号以,号相隔</param>
        /// <param name="body">短信内容</param>
        public static string SendMobileMSM(string mobile, string body)
        {
 
            string url = "http://utf8.sms.webchinese.cn/?Uid=xxxxxx&Key=xxxxxx&smsMob=" + mobile + "&smsText=" + body;
            string targeturl = url.Trim().ToString();
            try
            {
                HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
                hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                hr.Method = "GET";
                hr.Timeout = 30 * 60 * 1000;
                WebResponse hs = hr.GetResponse();
                Stream sr = hs.GetResponseStream();
                StreamReader ser = new StreamReader(sr, Encoding.Default);
                return GetMobileMSMStatus(ser.ReadToEnd());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

用“网建”平台发手机短信的C#代码

标签:msm   break   def   throw   namespace   字符   article   xxxxxx   搜索   

原文地址:https://www.cnblogs.com/niunan/p/8616175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!