2
votes

I am trying to read the Out Of Office status and message from other users in the organization from my c# program. We are running Exchange 2013 on premises.

This app is running as an Active Directory account (with it's own exchange mailbox), and I cannot use impersonation.

I have spent some time trying out the solutions to similar questions such as:

I'm trying to get something like:

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

Please help me understand, thank you.

3
Lame downvotes with no comments? How can I improve the question with no feedback?Aaron
I was looking into the same scenario with this being the most detailed post covering a selection of options. I've also no clue why anybody would have downvoted you since as you say no comments so no indication what's wrong.Myzifer
@Myzifer I was able to get an exact solution to my question that I now use and I just posted it as an answer. Hope that might help you.Aaron

3 Answers

4
votes

This is what I ended up using and it works.

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>[email protected]</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
votes

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 This one takes as a paramater, urlname and I'm not sure where that url is coming from. This one seems the most promising though, any ideas where that comes from?

urlname is just the EWS URL if you are using the Managed API than just use the value from service.url.

http://gsexdev.blogspot.com/2011/11/using-mailtips-in-ews-to-get-oof-out-of.html I don't have reference to ExchangeServiceBinding, even though I'm using Microsoft.Exchange.WebServices.Data;

This is proxy code generated from the Exchange Web Service WSDL file see https://msdn.microsoft.com/en-us/library/office/dd877040(v=exchg.140).aspx (Microsoft.Exchange.WebServices.Data) is the Managed API.

1
votes

Solution worked great for me as supplied by Aaron. However returning the substring gave me issues since ours was an html string rather than plain text and it didn't parse correctly. So i replaced the StreamReader down portion with the following. This still returns as a string but correctly parses as the html it was returned as.

        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();