公司一直使用騰訊提供的免費企業郵箱服務,今天用管理員帳戶登錄后發現,原來現在騰訊的企業郵箱也開放了部分API
你可以通過開放接口實現以下功能:
數據同步
數據同步可以幫助你同步部門成員信息,你還可以創建、刪除、修改帳號信息。
來信提醒
新郵件將即時在OA等辦公系統提醒你。
單點登錄
你可以從公司OA系統、網站一鍵進入企業郵箱,免去登錄過程。
具體的功能可以自己登陸騰訊企業郵箱管理員界面,進入“工具箱--開放協議”中下載開放協議文檔查看,有了這些API就能做不少事情了,可以嘗試和公司內部的一些系統進行整合,相信會有意想不到的驚喜。
簡單測試了一下未讀郵件數這個能力,代碼很簡單,就不做說明了,由於API返回的數據是JSON格式,用到了LitJSON
1 /// <summary> 2 /// POST時用到的幾個數據 3 /// </summary> 4 public struct PostData 5 { 6 /// <summary> 7 /// 目標服務器地址 8 /// </summary> 9 public string url; 10 /// <summary> 11 /// 采用的編碼 12 /// </summary> 13 public string encoding; 14 /// <summary> 15 /// POST的數據 16 /// </summary> 17 public string contentData; 18 /// <summary> 19 /// contentType 20 /// </summary> 21 public string contentType; 22 /// <summary> 23 /// 添加其他的Header 24 /// </summary> 25 public string header; 26 } 27 28 /// <summary> 29 /// 申請騰訊企業郵箱時的管理員賬戶 30 /// </summary> 31 string client_id = "hhhhhhhhhhh.com"; 32 /// <summary> 33 /// 騰訊企業郵箱分配的接口key 34 /// </summary> 35 string client_secret = "3ed4b10okd37f2e0f56f23a3b6e53013"; 36 /// <summary> 37 /// 目標郵箱的賬戶,獲取對應的未讀郵件數 38 /// </summary> 39 string alias = "chen123@hhhhhhhhhhh.com"; 40 41 /// <summary> 42 /// 按鈕事件,調用騰訊API獲取Token,然后獲取未讀郵件數 43 /// </summary> 44 /// <param name="sender"></param> 45 /// <param name="e"></param> 46 private void bNewMailCount_Click(object sender, EventArgs e) 47 { 48 //第一步獲取Token 49 PostData pdata = new PostData(); 50 pdata.url = "https://tel.exmail.qq.com/cgi-bin/token"; 51 pdata.contentData = "grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + client_secret; 52 pdata.encoding = "UTF-8"; 53 pdata.contentType = "application/x-www-form-urlencoded"; 54 55 string responseString = ""; 56 //獲取token_type和access_token 57 LitJson.JsonData json = this.Post(pdata, out responseString); 58 59 if (json != null) 60 { 61 //獲取Token成功后,第二步,獲取未讀郵件數 62 pdata = new PostData(); 63 pdata.url = "http://openapi.exmail.qq.com:12211/openapi/mail/newcount"; 64 pdata.contentData = "alias=" + alias; 65 pdata.encoding = "UTF-8"; 66 pdata.contentType = "application/x-www-form-urlencoded"; 67 pdata.header = "Authorization: " + json["token_type"] + " " + json["access_token"]; 68 69 responseString = ""; 70 json = this.Post(pdata, out responseString); 71 72 if (json != null) 73 { 74 responseString = "賬戶:" + json["Alias"] + " 未讀郵件:" + json["NewCount"]; 75 iHandler.TextBoxAppend(this.tResponse, responseString, true); 76 } 77 } 78 } 79 /// <summary> 80 /// POST,返回LitJson.JsonData對象 81 /// </summary> 82 /// <param name="pdata"></param> 83 /// <param name="responseString"></param> 84 /// <returns></returns> 85 private LitJson.JsonData Post(PostData pdata, out string responseString) 86 { 87 responseString = String.Empty; 88 try 89 { 90 Encoding encoding = Encoding.GetEncoding(pdata.encoding); 91 byte[] data = encoding.GetBytes(pdata.contentData); 92 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(pdata.url); 93 webReq.Method = "POST"; 94 webReq.ContentLength = data.Length; 95 webReq.ContentType = pdata.contentType + "; charset=" + pdata.encoding; 96 if (!String.IsNullOrEmpty(pdata.header)) 97 { 98 webReq.Headers.Add(pdata.header); 99 } 100 Stream webStream = webReq.GetRequestStream(); 101 webStream.Write(data, 0, data.Length); 102 103 WebResponse webResp = webReq.GetResponse(); 104 Stream webRespStream = webResp.GetResponseStream(); 105 StreamReader reader = new StreamReader(webRespStream, encoding); 106 string respXml = reader.ReadToEnd(); 107 reader.Close(); reader.Dispose(); 108 webResp.Close(); 109 110 responseString = respXml; 111 return LitJson.JsonMapper.ToObject(respXml); ; 112 } 113 catch (Exception ex) 114 { 115 responseString = ex.ToString(); 116 return null; 117 } 118 }