从Exchange服务器读取其他用户C#的办公室

时间:2015-08-24 20:30:20

标签: c# exchangewebservices exchange-server-2013

我正在尝试从我的c#程序中读取组织中其他用户的“外出”状态和消息。我们在内部运行Exchange 2013.

此应用程序作为Active Directory帐户运行(使用自己的Exchange邮箱),我无法使用模拟。

我花了一些时间尝试类似问题的解决方案,例如:

我正试图得到类似的东西:

public void checkOOF(string userEmail){
bool isOOF = checkstuff(userEmail);
string message;
if(isOOF)
   message = getOOFMessage(userEmail);
}

请帮助我理解,谢谢。

2 个答案:

答案 0 :(得分:4)

这是我最终使用它并且有效。

public static string getOOM(string emailToCheck) //needs to be full email of user.
{
            string EWSurl = String.Format("https://{0}/EWS/Exchange.asmx", ExchangePath);
            WebRequest webRequest = WebRequest.Create(EWSurl);
            HttpWebRequest httpwebRequest = (HttpWebRequest)webRequest;
            httpwebRequest.Method = "POST";
            httpwebRequest.ContentType = "text/xml; charset=utf-8";
            httpwebRequest.ProtocolVersion = HttpVersion.Version11;
            httpwebRequest.Credentials = new NetworkCredential("user", "password", "domain");//service Account
            httpwebRequest.Timeout = 60000;
            Stream requestStream = httpwebRequest.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);

            StringBuilder getMailTipsSoapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
            getMailTipsSoapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
            getMailTipsSoapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
            getMailTipsSoapRequest.Append("xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\"><soap:Header>");
            getMailTipsSoapRequest.Append(" <t:RequestServerVersion Version=\"Exchange2010\"/></soap:Header><soap:Body>");
            getMailTipsSoapRequest.Append("<GetMailTips xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">");
            getMailTipsSoapRequest.Append("<SendingAs>");
            getMailTipsSoapRequest.Append("<t:EmailAddress>accessingemail@domain.com</t:EmailAddress>");
            getMailTipsSoapRequest.Append("<t:RoutingType>SMTP</t:RoutingType></SendingAs>");
            getMailTipsSoapRequest.Append("<Recipients><t:Mailbox>");
            getMailTipsSoapRequest.Append("<t:EmailAddress>" + emailToCheck + "</t:EmailAddress>");
            getMailTipsSoapRequest.Append("<t:RoutingType>SMTP</t:RoutingType></t:Mailbox></Recipients>");
            getMailTipsSoapRequest.Append(" <MailTipsRequested>OutOfOfficeMessage</MailTipsRequested></GetMailTips>");
            getMailTipsSoapRequest.Append("</soap:Body></soap:Envelope>");

            streamWriter.Write(getMailTipsSoapRequest.ToString());
            streamWriter.Close();
            HttpWebResponse webResponse = (HttpWebResponse)httpwebRequest.GetResponse();

            StreamReader streamreader = new StreamReader(webResponse.GetResponseStream());
            string response = streamreader.ReadToEnd();
            if (response.Contains("<t:Message/>"))
                return null;
            int messageIndex = response.IndexOf("<t:Message>");
            response = response.Substring(messageIndex, response.IndexOf("</t:Message>") - messageIndex);
            return response;
}

答案 1 :(得分:1)

由亚伦提供的解决方案对我来说非常有效。但是,返回子字符串给我带来了问题,因为我们的字符串是html字符串而不是纯文本,并且无法正确解析。因此,我用以下内容替换了StreamReader的下部。仍然会以字符串形式返回,但正确解析为返回的html。

        XDocument doc;
        using (Stream responseStream = response.GetResponseStream())
        {
            doc = XDocument.Load(responseStream);
        }
        return doc.Root.Descendants().Where(d => d.Name.LocalName == "Message").Select(d => d.Value).FirstOrDefault();