2

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

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

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

我试图得到类似的东西:

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

请帮我理解,谢谢。

4

3 回答 3

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;
}
于 2017-08-30T16:14:07.307 回答
1

Aaron 提供的解决方案对我来说非常有用。然而,返回子字符串给了我一些问题,因为我们的是一个 html 字符串而不是纯文本,并且它没有正确解析。所以我用以下内容替换了 StreamReader down 部分。这仍然作为字符串返回,但正确地解析为它返回的 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();
于 2018-06-27T17:24:44.693 回答
1

http://blogs.msdn.com/b/devmsg/archive/2014/06/03/ews-how-to-retrieve-the-oof-out-of-facility-settings-message-using-ews-for- an-exchange-user.aspx这个以 urlname 作为参数,我不确定该 url 来自哪里。不过,这似乎是最有前途的,有什么想法来自哪里?

如果您使用托管 API,则 urlname 只是 EWS URL,而不仅仅是使用 service.url 中的值。

http://gsexdev.blogspot.com/2011/11/using-mailtips-in-ews-to-get-oof-out-of.html我没有参考 ExchangeServiceBinding,即使我使用的是 Microsoft。 Exchange.WebServices.Data;

这是从 Exchange Web 服务 WSDL 文件生成的代理代码,请参阅https://msdn.microsoft.com/en-us/library/office/dd877040(v=exchg.140).aspx (Microsoft.Exchange.WebServices.Data)是托管 API。

于 2015-08-25T04:20:02.640 回答