近端时间接了个需求在原来的OA办公系统中添加一个发送短信功能。(既然需要发送短信那肯定要申请一个发送短信的账号,我这里是以移动mas为列子)

c#的weserver需要选协议WS。其他的基本不用怎么填写直接提交就可以了。
在vs项目中添加服务器引用

其中需要选择高级中的web引用

添加完成后样式

调用就简单了
CmsLsMac.WsSmsServiceService cms=new CmsLsMac.WsSmsServiceService ();//短信接口
myContent mycontent = new myContent();
string md5= UserMd5("加密信息");
mycontent.ecName = "企业名称";
mycontent.apId = "账号";
mycontent.secretKey = "密码";
//mycontent.mobiles = "电话";
mycontent.content = "今天有雷阵雨";
mycontent.sign = "签名";
mycontent.addSerial = "";//可以为null
List<string> list = new List<string>();
list.Add("电话");//具体有哪些电话号码
string mobezz = "";
foreach (var item in list)
{
mobezz += item;
}
//md5加密
mycontent.mac = UserMd5(mycontent.ecName + mycontent.apId + mycontent.secretKey + mobezz + mycontent.content + mycontent.sign);
string str="<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<WsSubmitReq>" + " <apId>" + mycontent.apId + "</apId>" + " <secretKey>" + mycontent.secretKey + "</secretKey>"
+ " <ecName>" + mycontent.ecName + "</ecName>" + " <mobiles>" + GetMobiles(list)
+ " </mobiles>" + " <content>" + mycontent.content + "</content>" + " <sign>" + mycontent.sign + "</sign>" + " <addSerial>" + mycontent.addSerial + "</addSerial>"
+ " <mac>" + mobezz + "</mac>" + "</WsSubmitReq>";
var returnz= cms.sendSms(str);//发送短信方法 根据返回值查看是否发送成功。
最主要的是需要知道接口那个方法是发送短信的。
MD5加密
public string UserMd5(string str)
{
string cl = str;
string pwd = "";
MD5 md5 = MD5.Create();//实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("x2");
}
return pwd;
}
电话号码循环的---可群发
public string GetMobiles(List<string> list)
{
StringBuilder soap = new StringBuilder();
foreach (var item in list)
{
soap.Append("<string>" + item + "</string>");
}
return soap.ToString();
}
原文:https://www.cnblogs.com/bit-by-bit/p/11089616.html