先去企业微信门户网站获得密钥和应用ID

创建一个静态工具类

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp1
{
public static class SendWeChatMessage
{
private const string getTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
private const string sendMessageUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}";
private static readonly HttpClient httpClient = new HttpClient(); public async static Task<string> Send(WeChatParameter parameter)
{
string tokenUrl = string.Format(getTokenUrl, parameter.CorpId, parameter.CorpSecret);
var reponse = await httpClient.GetAsync(tokenUrl);
var str = await reponse.Content.ReadAsStringAsync();
var jObject = JObject.Parse(str);
var getTokenResult = jObject["errmsg"].ToString().ToLower();
var token = jObject["access_token"].ToString();
// jObject["errmsg"].ToString();
if (!getTokenResult.Equals("ok") || string.IsNullOrEmpty(token))
return "获取token失败";
string sendMsgUrl = string.Format(sendMessageUrl, token);
var sendContentModel = new
{
touser = parameter.Touser,
msgtype = parameter.MsgType,
agentid = parameter.AgentId,
text = new
{
content = parameter.Content
}
};
var sendContentStr = JsonConvert.SerializeObject(sendContentModel);
HttpContent content = new StringContent(sendContentStr, Encoding.UTF8);
var response = await httpClient.PostAsync(sendMsgUrl, content);
return await response.Content.ReadAsStringAsync();
} } public class WeChatParameter
{
public string CorpId { get; set; }
public string CorpSecret { get; set; }
public string Content { get; set; }
public string Touser { get; set; }
public string MsgType { get; set; } = "text";
public string AgentId { get; set; }
}
}

返回结果是json字符串,需要不同结果的同学可以自己修改一下返回类型。(当返回的结果中是“errmsg”:"ok",代表成功了)

C#给企业微信中的成员发送消息的更多相关文章

  1. XMLDocument 方法中实现post发送消息

    XMLDocument 方法中实现post发送消息

  2. 原创:【微信小程序】发送消息模板教程(后台以PHP示例)

    1.本教程对外开放,未经博主同意,禁止转载. 2.准备材料:1)公众号|小程序,添加选择的模板消息,2)在设置>开发设置页面,开通消息模板功能:如: 3.因为调用微信发送模板的接口是:https ...

  3. makeObjectsPerformSelector对数组中的对象发送消息执行对象中方法

    - (void)makeObjectsPerformSelector:(SEL)aSelector; - (void)makeObjectsPerformSelector:(SEL)aSelector ...

  4. Java企业微信开发_05_消息推送之发送消息(主动)

    一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息之后,微信服务器将消息传递给 第三方服务器,第三方服务器接 ...

  5. Java企业微信开发_04_消息推送之发送消息(主动)

    源码请见: Java企业微信开发_00_源码及资源汇总贴 一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息 ...

  6. Java发送企业微信应用消息

    1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息之后,微信服务器将消息传递给 第三方服务器,第三方服务器接收到消息后,再 ...

  7. 参照企业微信审批业务,在Winform开发框架中工作流模块的实现业务审批

    目前微信的企业号已经切换到企业微信里面,这个是一个APP程序,提供了很丰富的企业应用,其中包括了业务审批处理,审批业务包括请假.报销.费用.出差等很多个审批场景,在Winform开发框架中工作流模块这 ...

  8. odoo发送信息到微信公众平台、企业微信

    目录 odoo发送信息到微信 @(odoo client.message.send_text) odoo发送信息到微信 在odoo平台中进行项目开发的时候有时会用到跟其他平台对接发送信息. 这里我写一 ...

  9. 【转】odoo 10的企业微信发送程序介绍

    本文介绍的微信发送程序不是独立的模块,是某企业应用的一部分, 源码可在京津冀odoo技术交流群的群共享中下载.   [1]基本配置 在work.weixin.qq.com上注册一个企业后,会得到企业的 ...

随机推荐

  1. 多个python版本共存时的pip配置

    两种方法来配置pip Func1: 1.1 找到python环境的安装包,将python.exe文件重命名,如:将python2.7版本的python.exe重命名为Python2.exe,将pyth ...

  2. Odoo配置文件

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189223.html

  3. Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip

    目录 Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip 场景 过程还原 结论 参考资料 Nginx+Docker部署模式下 asp.net core 获取真实的客户端 ...

  4. 关于使用repo时repo init和repo sync失败的一个解决方案

    由于我国的网络的原因,在访问谷歌等一些国外网络资源时经常会遇到被拦截的情况,导致repo等一些代码管理工具拉取代码网络受限受阻,下面提供一个可以参考的简单解决方案. 1.repo init时的遇到fa ...

  5. flask POOL,websocket握手

    一.POOL Pool就是为了多线程访问数据库,减少数据库压力 回顾pymysql import pymysql #建立连接 mysql_conn = pymysql.connect(host=&qu ...

  6. JS高阶---作用域与执行上下文

    一句话介绍 .

  7. 石欣钰-201871010117 《面向对象程序设计(java)》第六、七周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/ ...

  8. 201871010131-张兴盼《面向对象程序设计(java)》第十六周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  9. Nginx ServerName 配置说明

    Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活,如果你要做多用户博客,那么每个用户拥有自己的二级域名也就很容易实现了.下面我就来说说server_name的使用吧:ser ...

  10. LG5201 「USACO2019JAN」Shortcut 最短路树

    \(\mathrm{Shortcut}\) 问题描述 LG5201 题解 最短路树. 显然奶牛的路径就是从\(1\)走到各个草地,于是从\(1\)跑最短路,构建最短路树. 为了保证字典序,从\(1\) ...